Skip to content

Commit e21465e

Browse files
committed
Merge branch 'master' of github.com:V1ncentzzZ/leetcode
2 parents 1a09a80 + f73b757 commit e21465e

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[103. 二叉树的锯齿形层次遍历](https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/)
2+
3+
给定一个二叉树,返回其节点值的锯齿形层次遍历。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
4+
5+
例如:
6+
给定二叉树 [3,9,20,null,null,15,7],
7+
8+
3
9+
/ \
10+
9 20
11+
/ \
12+
15 7
13+
返回锯齿形层次遍历如下:
14+
15+
[
16+
[3],
17+
[20,9],
18+
[15,7]
19+
]
20+
21+
```
22+
/**
23+
* Definition for a binary tree node.
24+
* public class TreeNode {
25+
* int val;
26+
* TreeNode left;
27+
* TreeNode right;
28+
* TreeNode(int x) { val = x; }
29+
* }
30+
*/
31+
class Solution {
32+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
33+
List<List<Integer>> result = new ArrayList<>();
34+
if(root == null) return result;
35+
executor(root, result, 0);
36+
return result;
37+
}
38+
39+
public void executor(TreeNode node,List<List<Integer>> result,int high){
40+
if(node == null) return;
41+
if(result.size() <= high) result.add(new ArrayList<>());
42+
if(high % 2 == 0){
43+
result.get(high).add(node.val);
44+
}else {
45+
result.get(high).add(0,node.val);
46+
}
47+
executor(node.left, result, high+1);
48+
executor(node.right, result, high+1);
49+
}
50+
}
51+
```
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
[104. 二叉树的最大深度][https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/]
2+
3+
给定一个二叉树,找出其最大深度。
4+
5+
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
6+
7+
说明: 叶子节点是指没有子节点的节点。
8+
9+
示例:
10+
给定二叉树 [3,9,20,null,null,15,7]
11+
```
12+
3
13+
/ \
14+
9 20
15+
/ \
16+
15 7
17+
```
18+
返回它的最大深度 3 。
19+
20+
```
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* int val;
25+
* TreeNode left;
26+
* TreeNode right;
27+
* TreeNode(int x) { val = x; }
28+
* }
29+
*/
30+
class Solution {
31+
// public int maxDepth(TreeNode root) {
32+
// if(root == null) return 0;
33+
// return getDepth(root.left,root.right,1);
34+
// }
35+
36+
// public int getDepth(TreeNode node1,TreeNode node2,int high){
37+
// if(node1 == null && node2 == null) return high;
38+
// high++;
39+
// if(node1 == null) return getDepth(node2.left,node2.right,high);
40+
// if(node2 == null) return getDepth(node1.left,node1.right,high);
41+
// return Math.max(getDepth(node1.left,node1.right,high),getDepth(node2.left,node2.right,high));
42+
// }
43+
44+
public int maxDepth(TreeNode root) {
45+
if(root == null) return 0;
46+
int left = maxDepth(root.left);
47+
int right = maxDepth(root.right);
48+
return 1 + (left > right ? left : right);
49+
}
50+
}
51+
```

0 commit comments

Comments
 (0)