LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)
2017-08-27 09:18
615 查看
Given inorder and postorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
题目标签:Array, Tree
这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order。因为post-order是最后一个数字是root,所以要从右向左的遍历。还需要把helper function 里的 right child 和 left child 顺序更换一下,并且要把相关的代入值也改一下。具体可以看code。
Java Solution:
Runtime beats 68.55%
完成日期:08/26/2017
关键词:Array, Tree
关键点:递归;利用post-order 和 in-order 的位置关系递归
/** * 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) { Map<Integer, Integer> inMap = new HashMap<Integer, Integer>(); // save inorder number as key, position as value into map for(int i=0; i<inorder.length; i++) inMap.put(inorder[i], i); TreeNode root = helper(postorder, postorder.length-1, 0, inorder.length - 1, inMap); return root; } public TreeNode helper(int[] postorder, int postEnd, int inStart, int inEnd, Map<Integer, Integer> inMap) { if(inStart > inEnd) return null; int rootVal = postorder[postEnd]; TreeNode root = new TreeNode(rootVal); int inRoot = inMap.get(rootVal); // position in inOrder /* inStart & inEnd: for left child, move inEnd to the left of root * for right child, move inStart to the right of root */ root.right = helper(postorder, postEnd - 1, inRoot + 1, inEnd, inMap); /* postStart: for right left, go to inorder to check how many right children does root have, * add it into postorder to skip them to reach left child */ root.left = helper(postorder, postEnd - (inEnd - inRoot) - 1, inStart, inRoot - 1, inMap); return root; } }
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
相关文章推荐
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium
- Leetcode - Tree - 106. Construct Binary Tree from Inorder and Postorder Traversal(根据中序遍历和后序遍历重构二叉树)
- Leetcode 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] 105. Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
- Construct Binary Tree from Inorder and Postorder Traversal ——通过中序、后序遍历得到二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序遍历二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序重建二叉树@LeetCode
- LeetCode(Construct Binary Tree from Inorder and Postorder Traversal)根据二叉树的中序和后续构造二叉树
- LeetCode-Construct Binary Tree from Inorder and Postorder Traversal-中序后序重构二叉树-递归
- LeetCode OJ:Construct Binary Tree from Inorder and Postorder Traversal(从中序以及后序遍历结果中构造二叉树)
- [LeetCode]109. Construct Binary Tree from Inorder and Postorder Traversal由中序序列和后序序列重建二叉树
- Construct Binary Tree from Inorder and Postorder Traversal(用中序和后序建树,在题目给定的函数中完成) 【leetcode】
- LeetCode(Construct Binary Tree from Preorder and Inorder Traversal )根据二叉树的中序遍历和后序遍历重建二叉树
- Construct Binary Tree from Inorder and Postorder Traversal 中序和后序重建二叉树
- [Leetcode] Construct binary tree from inorder and postorder travesal 利用中序和后续遍历构造二叉树