Construct Binary Tree from Inorder and Postorder Traversal Java
2014-08-19 17:01
417 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Key to Solve: Recursion + HashMap(Key:inorder's num, Value:position )
Similar idea as
Construct Binary Tree from Preorder and Inorder Traversal
beside the top root start from back.
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length==0 || postorder.length==0) return null;
HashMap<Integer, Integer> map=new HashMap<Integer,Integer>();
//construct map by value and position of inoder
for(int i=0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return helperRecur(postorder,inorder,0,postorder.length-1,
0,inorder.length-1,map);
}
private TreeNode helperRecur(int[] postorder,int[] inorder, int postL,
int postR,int inL,int inR,HashMap<Integer,Integer> map){
if(postL>postR || inL>inR){
return null;
}
TreeNode root=new TreeNode(postorder[postR]);
int rootIndex=map.get(root.val);
int distance=rootIndex-inL;
root.left=helperRecur(postorder,inorder,postL,postL+distance-1,
inL,rootIndex-1,map);
root.right=helperRecur(postorder,inorder,postL+distance,postR-1,
rootIndex+1,inR,map);
return root;
}
}
Note:
You may assume that duplicates do not exist in the tree.
Key to Solve: Recursion + HashMap(Key:inorder's num, Value:position )
Similar idea as
Construct Binary Tree from Preorder and Inorder Traversal
beside the top root start from back.
public class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length==0 || postorder.length==0) return null;
HashMap<Integer, Integer> map=new HashMap<Integer,Integer>();
//construct map by value and position of inoder
for(int i=0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return helperRecur(postorder,inorder,0,postorder.length-1,
0,inorder.length-1,map);
}
private TreeNode helperRecur(int[] postorder,int[] inorder, int postL,
int postR,int inL,int inR,HashMap<Integer,Integer> map){
if(postL>postR || inL>inR){
return null;
}
TreeNode root=new TreeNode(postorder[postR]);
int rootIndex=map.get(root.val);
int distance=rootIndex-inL;
root.left=helperRecur(postorder,inorder,postL,postL+distance-1,
inL,rootIndex-1,map);
root.right=helperRecur(postorder,inorder,postL+distance,postR-1,
rootIndex+1,inR,map);
return root;
}
}
相关文章推荐
- Construct Binary Tree from Inorder and Postorder Traversal Traversal leetcode java
- Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal
- [JAVA]LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal | Java最短代码实现
- [leetcode-106]Construct Binary Tree from Inorder and Postorder Traversal(java)
- Leetcode oj java Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]106. Construct Binary Tree from Inorder and Postorder Traversal@Java结题报告
- 【LeetCode-面试算法经典-Java实现】【106-Construct Binary Tree from Inorder and Postorder Traversal(构造二叉树II)】
- [LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal
- leetcode-java-106. Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode] Construct Binary Tree from Inorder and Postorder Traversal (Java)
- Construct Binary Tree from Inorder and Postorder Traversal (Java)
- LeetCode_Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode 79] 106 Construct Binary Tree from Inorder and Postorder Traversal
- [leetcode刷题系列]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal
- leetcode - Construct Binary Tree from Inorder and Postorder Traversal