FTPrep, 106 Construct Binary Tree from Inorder and Postorder Traversal
2017-10-07 12:41
316 查看
和105 是一个道理,套路是一样一样的。
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length==0 || postorder.length==0) return null;
HashMap<Integer, Integer> indexInInorderMap= new HashMap<>();
for(int i =0; i<inorder.length; i++) indexInInorderMap.put(inorder[i], i);
return constructTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1, indexInInorderMap);
}
private TreeNode constructTree(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight, HashMap<Integer, Integer> map){
if(inLeft>inRight || postLeft>postRight) return null;
int rootIndexInInorder= map.get(postorder[postRight]);
TreeNode root=new TreeNode(postorder[postRight]);
root.left=constructTree(inorder, inLeft, rootIndexInInorder-1, postorder, postLeft, postLeft+rootIndexInInorder-inLeft-1, map);
root.right=constructTree(inorder, rootIndexInInorder+1, inRight , postorder, postLeft+rootIndexInInorder-inLeft, postRight-1, map);
return root;
}
}
// 关键之处!!!重中之重,就是递归时候的 左右index,inorder里面的index的好划分,因为就是一刀切,分两边。容易错的是:preorder/postorder里的片段是根据inorder里的左右两边长度来划分的,所以!!一定要长度相等,一定长度相等!!
代码:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(inorder.length==0 || postorder.length==0) return null;
HashMap<Integer, Integer> indexInInorderMap= new HashMap<>();
for(int i =0; i<inorder.length; i++) indexInInorderMap.put(inorder[i], i);
return constructTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1, indexInInorderMap);
}
private TreeNode constructTree(int[] inorder, int inLeft, int inRight, int[] postorder, int postLeft, int postRight, HashMap<Integer, Integer> map){
if(inLeft>inRight || postLeft>postRight) return null;
int rootIndexInInorder= map.get(postorder[postRight]);
TreeNode root=new TreeNode(postorder[postRight]);
root.left=constructTree(inorder, inLeft, rootIndexInInorder-1, postorder, postLeft, postLeft+rootIndexInInorder-inLeft-1, map);
root.right=constructTree(inorder, rootIndexInInorder+1, inRight , postorder, postLeft+rootIndexInInorder-inLeft, postRight-1, map);
return root;
}
}
// 关键之处!!!重中之重,就是递归时候的 左右index,inorder里面的index的好划分,因为就是一刀切,分两边。容易错的是:preorder/postorder里的片段是根据inorder里的左右两边长度来划分的,所以!!一定要长度相等,一定长度相等!!
相关文章推荐
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
- leetcode[106]Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历构造二叉树)
- LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal
- [Leetcode 79] 106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 106 —— Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode106—Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode]*106.Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal
- leetcode 106: Construct Binary Tree from Inorder and Postorder Traversal
- 106. Construct Binary Tree from Inorder and Postorder Traversal(unsolved)
- 【LeetCode】C# 106、Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal
- divided-and-conquer-106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 105, 106. Construct Binary Tree from Preorder and Inorder/Inorder and Postorder Traversal
- LeetCode OJ 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode——106. Construct Binary Tree from Inorder and Postorder Traversal