14
14
``` js
15
15
function TreeNode (val ){
16
16
this .val = val;
17
- this .left = this .right = null ;
17
+ this .left = null ;
18
+ this .right = null ;
18
19
}
19
20
```
20
21
@@ -39,7 +40,7 @@ const binaryTree = function (array) {
39
40
return buildTreeByArray (array, 0 );
40
41
}
41
42
42
- let arr = [1 ,2 ,3 ,null ,4 ,5 ,null ,null ,null ,6 ,7 ];
43
+ const arr = [1 ,2 ,3 ,null ,4 ,5 ,null ,null ,null ,6 ,7 ];
43
44
let root = binaryTree (arr);
44
45
```
45
46
@@ -59,15 +60,15 @@ function preOrder(root) {
59
60
##### 前序非递归
60
61
61
62
``` js
62
- var preOrderTraversal = function (root ) {
63
+ const preOrderTraversal = function (root ) {
63
64
if (root === null ){
64
65
return [];
65
66
}
66
- let res = [];
67
- let stack = [];
67
+ const res = [];
68
+ const stack = [];
68
69
stack .push (root);
69
70
while (stack .length !== 0 ){
70
- let node = stack .pop ();
71
+ const node = stack .pop ();
71
72
res .push (node .val );
72
73
if (node .right !== null ) {
73
74
stack .push (node .right );
@@ -96,12 +97,12 @@ function inOrder(root){
96
97
##### 中序非递归
97
98
98
99
``` js
99
- var inOrderTraversal = function (root ){
100
+ const inOrderTraversal = function (root ){
100
101
if (root === null ) {
101
102
return [];
102
103
}
103
- let res = [];
104
- let stack = [];
104
+ const res = [];
105
+ const stack = [];
105
106
let node = root;
106
107
while (stack .length !== 0 || node!== null ){
107
108
while (node !== null ){
@@ -132,12 +133,12 @@ function postOrder(root){
132
133
##### 后序非递归
133
134
134
135
``` js
135
- var postOrderTraversal = function (root ){ // 翻转非递归 后序遍历
136
+ const postOrderTraversal = function (root ){ // 翻转非递归 后序遍历
136
137
if (root === null ) {
137
138
return [];
138
139
}
139
- let res = [];
140
- let stack = [];
140
+ const res = [];
141
+ const stack = [];
141
142
stack .push (root);
142
143
while (stack .length !== 0 ){
143
144
let node = stack .pop ();
@@ -153,16 +154,16 @@ var postOrderTraversal = function(root){ //翻转非递归 后序遍历
153
154
}
154
155
```
155
156
156
- ##### 深度遍历
157
+ ##### 深度搜索
157
158
158
159
``` js
159
- var dfsUpToDown = function (root ){ // 递归,从上到下
160
- let res = [];
160
+ const dfsUpToDown = function (root ){ // 递归,从上到下
161
+ const res = [];
161
162
dfs (root, res);
162
163
return res;
163
164
}
164
165
165
- var dfs = function (node , res ){
166
+ const dfs = function (node , res ){
166
167
if (node === null ) {
167
168
return null ;
168
169
}
@@ -171,12 +172,12 @@ var dfs = function(node, res){
171
172
dfs (node .right , res);
172
173
}
173
174
174
- var dfsDownToUp = function (root ){ // 从下到上
175
+ const dfsDownToUp = function (root ){ // 从下到上
175
176
return divideAndConquer (root);
176
177
}
177
178
178
- var divideAndConquer = function (node ){ // 分治法
179
- let res = [];
179
+ const divideAndConquer = function (node ){ // 分治法
180
+ const res = [];
180
181
if (node === null ) {
181
182
return null ;
182
183
}
@@ -193,12 +194,12 @@ var divideAndConquer = function(node){ // 分治法
193
194
}
194
195
```
195
196
196
- ##### 广度遍历
197
+ ##### 广度搜索
197
198
198
199
``` js
199
- var bfs = function (root ){
200
+ const bfs = function (root ){
200
201
let res = [];
201
- let queue = [];
202
+ const queue = [];
202
203
queue .push (root);
203
204
while (queue .length !== 0 ){
204
205
let node = queue .shift ();
@@ -214,7 +215,7 @@ var bfs = function(root){
214
215
}
215
216
```
216
217
217
- ##### 104.二叉树的最大深度 [ 二叉树的最大深度] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ )
218
+ ##### 104.[ 二叉树的最大深度] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/ )
218
219
219
220
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。** 说明:** 叶子节点是指没有子节点的节点。
220
221
@@ -229,11 +230,68 @@ var bfs = function(root){
229
230
> 返回它的最大深度 3 。
230
231
231
232
``` js
232
- var maxDepth = function (root ) { // 递归
233
+ const maxDepth = function (root ) { // 递归
233
234
if (root === null ){
234
235
return 0 ;
235
236
}
236
- return Math .max (maxDepth (root .left ), maxDepth (root .right ))+ 1 ;
237
+ return Math .max (maxDepth (root .left ), maxDepth (root .right ))+ 1 ;
238
+ };
239
+ ```
240
+
241
+ 110.[ 平衡二叉树] ( https://leetcode-cn.com/problems/balanced-binary-tree/ )
242
+
243
+ 给定一个二叉树,判断它是否是高度平衡的二叉树。
244
+
245
+ ``` js
246
+ const isBalanced = function (root ) {
247
+ if (maxDepth (root) === - 1 ) {
248
+ return false
249
+ }
250
+ return true
251
+ };
252
+
253
+ const maxDepth = function (root ) {
254
+ if (root === null ) {
255
+ return 0
256
+ }
257
+ const left = maxDepth (root .left )
258
+ const right = maxDepth (root .right )
259
+ if (left === - 1 || right === - 1 || Math .abs (right - left) > 1 ) { // 判断左右子树的高度
260
+ return - 1
261
+ }
262
+ if (left > right) {
263
+ return left + 1
264
+ }else {
265
+ return right + 1
266
+ }
267
+
268
+ }
269
+ ```
270
+
271
+ 124.[ 二叉树中的最大路径和] ( https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/ )
272
+
273
+ ** 路径** 被定义为一条从树中任意节点出发,沿父节点—子节点连接,达到任意节点的序列。同一个节点在一条路径序列中 ** 至多出现一次** 。该路径 ** 至少包含一个** 节点,且不一定经过根节点。
274
+
275
+ ** 路径和** 是路径中各节点值的总和。
276
+
277
+ 给你一个二叉树的根节点 ` root ` ,返回其 ** 最大路径和** 。
278
+
279
+ > 思路:分治法,分为三种情况:左子树最大路径和最大,右子树最大路径和最大,左右子树最大加根节点最大,需要保存两个变量:一个保存子树最大路径和,一个保存左右加根节点和,然后比较这个两个变量选择最大值即可
280
+
281
+ ``` js
282
+ var maxPathSum = function (root ) {
283
+ let maxSum = Number .MIN_SAFE_INTEGER
284
+ function maxGain (node ) {
285
+ if (! node) {
286
+ return 0
287
+ }
288
+ const left = maxGain (node .left )
289
+ const right = maxGain (node .right )
290
+ maxSum = Math .max (maxSum, node .val , node .val + left + right, node .val + left, node .val + right)
291
+ return Math .max (node .val , node .val + left, node .val + right)
292
+ }
293
+ maxGain (root)
294
+ return maxSum
237
295
};
238
296
```
239
297
0 commit comments