Skip to content

Commit a536a0c

Browse files
committed
basics
1 parent 589a956 commit a536a0c

File tree

4 files changed

+177
-0
lines changed

4 files changed

+177
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package BinaryTreeLevelOrderTraversal;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/27/2015
11+
* Time: 14:56
12+
*
13+
* Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
14+
15+
For example:
16+
Given binary tree {3,9,20,#,#,15,7},
17+
3
18+
/ \
19+
9 20
20+
/ \
21+
15 7
22+
return its level order traversal as:
23+
[
24+
[3],
25+
[9,20],
26+
[15,7]
27+
]
28+
*/
29+
public class Solution {
30+
/**
31+
* bfs
32+
* @param root
33+
* @return
34+
*/
35+
public List<List<Integer>> levelOrder(TreeNode root) {
36+
List<List<Integer>> ret = new ArrayList<>();
37+
if(root==null)
38+
return ret;
39+
40+
TreeNode cur = root;
41+
List<TreeNode> q = new ArrayList<>();
42+
q.add(cur);
43+
while(!q.isEmpty()) {
44+
int l = q.size();
45+
List<Integer> currentLevel = new ArrayList<>();
46+
for(int i=0; i<l; i++) {
47+
cur = q.get(i);
48+
currentLevel.add(cur.val);
49+
if(cur.left!=null) q.add(cur.left);
50+
if(cur.right!=null) q.add(cur.right);
51+
}
52+
ret.add(currentLevel);
53+
q = q.subList(l, q.size()); // similar API as python
54+
}
55+
return ret;
56+
}
57+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package BinaryTreeLevelOrderTraversalII;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* User: Danyang
10+
* Date: 1/27/2015
11+
* Time: 15:03
12+
* Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level
13+
* by level from leaf to root).
14+
15+
For example:
16+
Given binary tree {3,9,20,#,#,15,7},
17+
3
18+
/ \
19+
9 20
20+
/ \
21+
15 7
22+
return its bottom-up level order traversal as:
23+
[
24+
[15,7],
25+
[9,20],
26+
[3]
27+
]
28+
*/
29+
public class Solution {
30+
/**
31+
* bfs
32+
* @param root
33+
* @return
34+
*/
35+
public List<List<Integer>> levelOrderBottom(TreeNode root) {
36+
List<List<Integer>> ret = new ArrayList<>();
37+
List<TreeNode> q = new ArrayList<>();
38+
if(root==null)
39+
return ret;
40+
41+
TreeNode cur = root;
42+
q.add(cur);
43+
while(!q.isEmpty()) {
44+
int l = q.size();
45+
List<Integer> level = new ArrayList<>();
46+
for(int i=0; i<l; i++) {
47+
cur = q.get(i);
48+
level.add(cur.val);
49+
if(cur.left!=null) q.add(cur.left);
50+
if(cur.right!=null) q.add(cur.right);
51+
}
52+
ret.add(0, level);
53+
q = q.subList(l, q.size());
54+
}
55+
return ret;
56+
}
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package MaximumDepthofBinaryTree;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/27/2015
8+
* Time: 15:17
9+
*
10+
* Given a binary tree, find its maximum depth.
11+
12+
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
13+
*/
14+
public class Solution {
15+
public int maxDepth(TreeNode root) {
16+
if(root==null)
17+
return 0;
18+
return getDepth(root, 1);
19+
}
20+
21+
int getDepth(TreeNode cur, int depth) {
22+
if(cur==null)
23+
return Integer.MIN_VALUE;
24+
if(cur.left==null && cur.right==null)
25+
return depth;
26+
return Math.max(getDepth(cur.left, depth+1), getDepth(cur.right, depth+1));
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package MinimumDepthofBinaryTree;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/27/2015
8+
* Time: 15:11
9+
*
10+
* Given a binary tree, find its minimum depth.
11+
12+
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
13+
14+
*/
15+
public class Solution {
16+
/**
17+
* Notice:
18+
* 1. must be leaf node
19+
* @param root
20+
* @return
21+
*/
22+
public int minDepth(TreeNode root) {
23+
if(root==null)
24+
return 0;
25+
return getDepth(root, 1);
26+
}
27+
28+
int getDepth(TreeNode cur, int depth) {
29+
if(cur==null)
30+
return Integer.MAX_VALUE;
31+
if(cur.left==null && cur.right==null)
32+
return depth;
33+
return Math.min(getDepth(cur.left, depth+1), getDepth(cur.right, depth+1));
34+
}
35+
}

0 commit comments

Comments
 (0)