Skip to content

Commit 06bc93e

Browse files
committed
Merge pull request Hearen#99 from cxxly/chenxiaoxu
update
2 parents a2c6497 + af13a22 commit 06bc93e

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# 103. Binary Tree Zigzag Level Order Traversal
2+
3+
## Problem
4+
5+
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).
6+
7+
For example:
8+
Given binary tree {3,9,20,#,#,15,7},
9+
3
10+
/ \
11+
9 20
12+
/ \
13+
15 7
14+
return its zigzag level order traversal as:
15+
[
16+
[3],
17+
[20,9],
18+
[15,7]
19+
]
20+
21+
tag:
22+
- bfs
23+
24+
## Solution
25+
26+
广度优先搜索,用一个变量纪录层数,奇数层,结果加入子链表尾部,偶数层加入子链表头部
27+
28+
**java**
29+
```java
30+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
31+
List<List<Integer>> res = new LinkedList<List<Integer>>();
32+
if(root==null) return res;
33+
Queue<TreeNode> q = new LinkedList<TreeNode>();
34+
q.offer(root);
35+
boolean level = false;
36+
while(!q.isEmpty()) {
37+
int levelNum = q.size();
38+
List<Integer> subList = new LinkedList<Integer>();
39+
for(int i=0; i<levelNum; i++) {
40+
if(q.peek().left!=null) q.offer(q.peek().left);
41+
if(q.peek().right!=null) q.offer(q.peek().right);
42+
if(level) subList.add(0, q.poll().val);
43+
else subList.add(q.poll().val);
44+
}
45+
res.add(subList);
46+
level = !level;
47+
}
48+
return res;
49+
}
50+
```
51+
52+
**go**
53+
```go
54+
55+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 104. Maximum Depth of Binary Tree
2+
3+
## Problem
4+
Given a binary tree, find its maximum depth.
5+
6+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
7+
8+
tag:
9+
10+
## Solution
11+
12+
最大树高=左右子树种较高的一个+1
13+
14+
**java**
15+
```java
16+
public int maxDepth(TreeNode root) {
17+
if(root==null) return 0;
18+
return Math.max(maxDepth(root.left), maxDepth(root.right))+1;
19+
}
20+
```
21+
22+
**go**
23+
```go
24+
25+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# 111. Minimum Depth of Binary Tree
2+
3+
## Problem
4+
5+
Given a binary tree, find its minimum depth.
6+
7+
tag:
8+
9+
## Solution
10+
11+
当前树最小高度等于:
12+
- 若左子树为空, 右子树高度+1
13+
- 若有子树为空, 左子树高度+1
14+
- 左右子树种最小高度+1
15+
16+
**java**
17+
```java
18+
public int minDepth(TreeNode root) {
19+
if(root==null) return 0;
20+
if(root.left==null) return minDepth(root.right)+1;
21+
if(root.right==null) return minDepth(root.left)+1;
22+
return Math.min(minDepth(root.left), minDepth(root.right))+1;
23+
}
24+
```
25+
26+
**go**
27+
```go
28+
29+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# 94. Binary Tree Inorder Traversal
2+
3+
## Problem
4+
5+
Given a binary tree, return the inorder traversal of its nodes' values.
6+
7+
```
8+
/**
9+
* Definition for a binary tree node.
10+
* public class TreeNode {
11+
* int val;
12+
* TreeNode left;
13+
* TreeNode right;
14+
* TreeNode(int x) { val = x; }
15+
* }
16+
*/
17+
```
18+
19+
tag:
20+
21+
## Solution
22+
23+
### 递归
24+
25+
**java**
26+
```java
27+
public List<Integer> inorderTraversal(TreeNode root, List<Integer> res) {
28+
if(root!=null) {
29+
inorderTraversal(root.left, res);
30+
res.add(root.val);
31+
inorderTraversal(root.right, res);
32+
}
33+
return res;
34+
}
35+
```
36+
37+
**go**
38+
```go
39+
40+
```
41+
42+
### 迭代
43+
44+
``` java
45+
public List<Integer> inorderTraversal(TreeNode root) {
46+
List<Integer> res = new LinkedList<Integer>();
47+
Stack<TreeNode> s = new Stack<TreeNode>();
48+
TreeNode p = root;
49+
while(p!=null || !s.isEmpty()) {
50+
//push root to stack, traverse left subtree
51+
if(p!=null) {
52+
s.push(p);
53+
p=p.left;
54+
} else {
55+
//pop root, visit root node, and travese right subtree
56+
p = s.pop();
57+
res.add(p.val);
58+
p = p.right;
59+
}
60+
}
61+
return res;
62+
}
63+
```

0 commit comments

Comments
 (0)