Skip to content

Commit 546b431

Browse files
committed
题110,124
1 parent c13e38e commit 546b431

File tree

2 files changed

+84
-26
lines changed

2 files changed

+84
-26
lines changed

入门/力扣经典算法.md renamed to 入门(Java)/力扣经典算法.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public void traversePostOrderWithoutRecursion() { //后序的递归后续理解
136136

137137
while (!stack.isEmpty()) {
138138
current = stack.peek();
139-
boolean hasChild = (current.left != null || current.right != null);//!!!
139+
boolean hasChild = (current.left != null || current.right != null);
140140
boolean isPrevLastChild = (prev == current.right ||
141141
(prev == current.left && current.right == null));
142142

数据结构篇/二叉树.md

Lines changed: 83 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
```js
1515
function TreeNode(val){
1616
this.val = val;
17-
this.left = this.right = null;
17+
this.left = null;
18+
this.right = null;
1819
}
1920
```
2021

@@ -39,7 +40,7 @@ const binaryTree = function (array) {
3940
return buildTreeByArray(array, 0);
4041
}
4142

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];
4344
let root = binaryTree(arr);
4445
```
4546

@@ -59,15 +60,15 @@ function preOrder(root) {
5960
##### 前序非递归
6061

6162
```js
62-
var preOrderTraversal = function (root) {
63+
const preOrderTraversal = function (root) {
6364
if(root === null){
6465
return [];
6566
}
66-
let res = [];
67-
let stack = [];
67+
const res = [];
68+
const stack = [];
6869
stack.push(root);
6970
while(stack.length !== 0){
70-
let node = stack.pop();
71+
const node = stack.pop();
7172
res.push(node.val);
7273
if (node.right !== null) {
7374
stack.push(node.right);
@@ -96,12 +97,12 @@ function inOrder(root){
9697
##### 中序非递归
9798

9899
```js
99-
var inOrderTraversal = function(root){
100+
const inOrderTraversal = function(root){
100101
if (root === null) {
101102
return [];
102103
}
103-
let res = [];
104-
let stack = [];
104+
const res = [];
105+
const stack = [];
105106
let node = root;
106107
while(stack.length!==0 || node!==null){
107108
while(node !== null){
@@ -132,12 +133,12 @@ function postOrder(root){
132133
##### 后序非递归
133134

134135
```js
135-
var postOrderTraversal = function(root){ //翻转非递归 后序遍历
136+
const postOrderTraversal = function(root){ //翻转非递归 后序遍历
136137
if (root === null) {
137138
return [];
138139
}
139-
let res = [];
140-
let stack = [];
140+
const res = [];
141+
const stack = [];
141142
stack.push(root);
142143
while(stack.length !== 0){
143144
let node = stack.pop();
@@ -153,16 +154,16 @@ var postOrderTraversal = function(root){ //翻转非递归 后序遍历
153154
}
154155
```
155156

156-
##### 深度遍历
157+
##### 深度搜索
157158

158159
```js
159-
var dfsUpToDown = function(root){ //递归,从上到下
160-
let res = [];
160+
const dfsUpToDown = function(root){ //递归,从上到下
161+
const res = [];
161162
dfs(root, res);
162163
return res;
163164
}
164165

165-
var dfs = function(node, res){
166+
const dfs = function(node, res){
166167
if (node === null) {
167168
return null;
168169
}
@@ -171,12 +172,12 @@ var dfs = function(node, res){
171172
dfs(node.right, res);
172173
}
173174

174-
var dfsDownToUp = function(root){ //从下到上
175+
const dfsDownToUp = function(root){ //从下到上
175176
return divideAndConquer(root);
176177
}
177178

178-
var divideAndConquer = function(node){ // 分治法
179-
let res = [];
179+
const divideAndConquer = function(node){ //分治法
180+
const res = [];
180181
if (node === null) {
181182
return null;
182183
}
@@ -193,12 +194,12 @@ var divideAndConquer = function(node){ // 分治法
193194
}
194195
```
195196

196-
##### 广度遍历
197+
##### 广度搜索
197198

198199
```js
199-
var bfs = function(root){
200+
const bfs = function(root){
200201
let res = [];
201-
let queue = [];
202+
const queue = [];
202203
queue.push(root);
203204
while(queue.length !== 0){
204205
let node = queue.shift();
@@ -214,7 +215,7 @@ var bfs = function(root){
214215
}
215216
```
216217

217-
##### 104.二叉树的最大深度[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
218+
##### 104.[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)
218219

219220
给定一个二叉树,找出其最大深度。二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。**说明:** 叶子节点是指没有子节点的节点。
220221

@@ -229,11 +230,68 @@ var bfs = function(root){
229230
> 返回它的最大深度 3 。
230231
231232
```js
232-
var maxDepth = function(root) { //递归
233+
const maxDepth = function(root) { //递归
233234
if(root === null){
234235
return 0;
235236
}
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
237295
};
238296
```
239297

0 commit comments

Comments
 (0)