Skip to content

Commit 1789f64

Browse files
committed
construct tree
1 parent 1e5b7e4 commit 1789f64

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package BinaryTreeZigzagLevelOrderTraversal;
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: 16:37
12+
*
13+
* Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then
14+
* right to left for the next level and alternate between).
15+
16+
For example:
17+
Given binary tree {3,9,20,#,#,15,7},
18+
3
19+
/ \
20+
9 20
21+
/ \
22+
15 7
23+
return its zigzag level order traversal as:
24+
[
25+
[3],
26+
[20,9],
27+
[15,7]
28+
]
29+
*/
30+
public class Solution {
31+
/**
32+
* bfs
33+
* @param root
34+
* @return
35+
*/
36+
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
37+
List<List<Integer>> ret = new ArrayList<>();
38+
List<TreeNode> q = new ArrayList<>();
39+
if(root==null)
40+
return ret;
41+
TreeNode cur = root;
42+
q.add(cur);
43+
boolean obverse = true;
44+
while(!q.isEmpty()) {
45+
int l = q.size();
46+
List<Integer> level = new ArrayList<>();
47+
for(int i=0; i<l; i++) {
48+
cur = q.get(i);
49+
if(obverse)
50+
level.add(cur.val);
51+
else
52+
level.add(0, cur.val);
53+
if(cur.left!=null) q.add(cur.left);
54+
if(cur.right!=null) q.add(cur.right);
55+
}
56+
ret.add(level);
57+
obverse = !obverse;
58+
q = q.subList(l, q.size());
59+
}
60+
return ret;
61+
}
62+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ConstructBinaryTreefromInorderandPostorderTraversal;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/27/2015
8+
* Time: 17:10
9+
*
10+
* Given inorder and postorder traversal of a tree, construct the binary tree.
11+
12+
Note:
13+
You may assume that duplicates do not exist in the tree.
14+
15+
*/
16+
public class Solution {
17+
/**
18+
* inorder: L c R
19+
* postorder: L R c
20+
* @param inorder
21+
* @param postorder
22+
* @return
23+
*/
24+
public TreeNode buildTree(int[] inorder, int[] postorder) {
25+
return buildTree(inorder, 0, inorder.length, postorder, 0, postorder.length);
26+
}
27+
28+
TreeNode buildTree(int[] in, int s1, int e1, int[] post, int s2, int e2) {
29+
if(s1>=e1 || s2>=e2)
30+
return null;
31+
TreeNode root = new TreeNode(post[e2-1]);
32+
int i;
33+
for(i=s1; i<e1 && in[i]!=root.val; i++);
34+
int len_l = i-s1;
35+
TreeNode l = buildTree(in, s1, i, post, s2, s2+len_l);
36+
TreeNode r = buildTree(in, i+1, e1, post, s2+len_l, e2-1);
37+
root.left = l;
38+
root.right = r;
39+
return root;
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ConstructBinaryTreefromPreorderandInorderTraversal;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/27/2015
8+
* Time: 16:50
9+
*
10+
* Given preorder and inorder traversal of a tree, construct the binary tree.
11+
12+
Note:
13+
You may assume that duplicates do not exist in the tree.
14+
*/
15+
public class Solution {
16+
/**
17+
* preorder c L R
18+
* inorder L c R
19+
* @param preorder
20+
* @param inorder
21+
* @return
22+
*/
23+
public TreeNode buildTree(int[] preorder, int[] inorder) {
24+
return buildTree(preorder, 0, preorder.length, inorder, 0, inorder.length);
25+
}
26+
27+
TreeNode buildTree(int[] preorder, int s1, int e1, int[] inorder, int s2, int e2) {
28+
if(s1>=e1 || s2>=e2)
29+
return null;
30+
TreeNode root = new TreeNode(preorder[s1]);
31+
int i = s2;
32+
for(; i<e2 && inorder[i]!=root.val; i++);
33+
int len_l = i-s2;
34+
TreeNode l = buildTree(preorder, s1+1, s1+1+len_l, inorder, s2, i);
35+
TreeNode r = buildTree(preorder, s1+1+len_l, e1, inorder, i+1, e2);
36+
root.left = l;
37+
root.right = r;
38+
return root;
39+
}
40+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ConvertSortedArraytoBinarySearchTree;
2+
3+
import commons.datastructures.TreeNode;
4+
5+
/**
6+
* User: Danyang
7+
* Date: 1/27/2015
8+
* Time: 16:46
9+
*
10+
* Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
11+
*/
12+
public class Solution {
13+
public TreeNode sortedArrayToBST(int[] num) {
14+
return toBST(num, 0, num.length);
15+
}
16+
17+
TreeNode toBST(int[] num, int s, int e) {
18+
if(s>=e)
19+
return null;
20+
int m = (s+e)/2;
21+
TreeNode cur = new TreeNode(num[m]);
22+
TreeNode l = toBST(num, s, m);
23+
TreeNode r = toBST(num, m+1, e);
24+
cur.left = l;
25+
cur.right = r;
26+
return cur;
27+
}
28+
}

0 commit comments

Comments
 (0)