Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal
2016-03-11 05:42
489 查看
105. Construct Binary Tree from Preorder and Inorder Traversal
Total Accepted: 58238 Total
Submissions: 206790 Difficulty: Medium
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
分析:
preorder的第一个节点是整个树的根,下一个节点便是root的左节点,而root的右节点需要知道root左子节点的个数,因此需要在inorder序列中找
该节点的index,则index-instart是左子节点的数目,加1算入root节点,这就是root右节点在preorder中相对于当前位置prestart的偏移量。
Method 1: 普通方法,遍历inorder数组寻找 目标index,然后分别调用递归赋值左右子节点。
Method 2: 网上的speed up版本,用了hashmap提高速度。
Method 3: 发现网上最快的2MS其实就是用了双向指针加速,并且找到index后直接break,于是改进了method 1。
因为标题不能过长,所以106. Construct Binary Tree from Inorder and Postorder Traversal 重开一文。
Total Accepted: 58238 Total
Submissions: 206790 Difficulty: Medium
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
分析:
1 / \ 2 3 / \ / \ 4 5 6 7 inorder: [4,2,5,1,6,3,7] preorder: [1,2,4,5,3,6,7] postorder: [4,5,2,6,7,3,1]
preorder的第一个节点是整个树的根,下一个节点便是root的左节点,而root的右节点需要知道root左子节点的个数,因此需要在inorder序列中找
该节点的index,则index-instart是左子节点的数目,加1算入root节点,这就是root右节点在preorder中相对于当前位置prestart的偏移量。
Method 1: 普通方法,遍历inorder数组寻找 目标index,然后分别调用递归赋值左右子节点。
public class Solution{ //23ms public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0, 0, inorder.length - 1, preorder, inorder); } public TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) { if (preStart > preorder.length - 1 || inStart > inEnd) { return null; } TreeNode root = new TreeNode(preorder[preStart]); int inIndex = 0; // Index of current root in inorder for (int i = inStart; i <= inEnd; i++) { if (inorder[i] == root.val) { inIndex = i; } } root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder); root.right = helper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder); return root; } }
Method 2: 网上的speed up版本,用了hashmap提高速度。
public class Solution{ //6ms public TreeNode buildTree(int[] preorder, int[] inorder) { Map<Integer, Integer> inMap = new HashMap<Integer, Integer>(); for(int i = 0; i < inorder.length; i++) { inMap.put(inorder[i], i); } TreeNode root = buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1, inMap); return root; } public TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd, Map<Integer, Integer> inMap) { if(preStart > preEnd || inStart > inEnd) return null; TreeNode root = new TreeNode(preorder[preStart]); int inRoot = inMap.get(root.val); int numsLeft = inRoot - inStart; root.left = buildTree(preorder, preStart + 1, preStart + numsLeft, inorder, inStart, inRoot - 1, inMap); root.right = buildTree(preorder, preStart + numsLeft + 1, preEnd, inorder, inRoot + 1, inEnd, inMap); return root; } }
Method 3: 发现网上最快的2MS其实就是用了双向指针加速,并且找到index后直接break,于是改进了method 1。
public class Solution{ //2ms public TreeNode buildTree(int[] preorder, int[] inorder) { return helper(0, 0, inorder.length - 1, preorder, inorder); } public TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) { if (preStart > preorder.length - 1 || inStart > inEnd) { return null; } TreeNode root = new TreeNode(preorder[preStart]); int inIndex = 0; // Index of current root in inorder for (int i = inStart, j=inEnd; i <= j; i++,j--) { if (inorder[i] == root.val) { inIndex = i; break; }else if(inorder[j] == root.val){ inIndex=j; break; } } root.left = helper(preStart + 1, inStart, inIndex - 1, preorder, inorder); root.right = helper(preStart + inIndex - inStart + 1, inIndex + 1, inEnd, preorder, inorder); return root; } }
因为标题不能过长,所以106. Construct Binary Tree from Inorder and Postorder Traversal 重开一文。
相关文章推荐
- vaadin 7 使用gwt 发布js和css
- Webstrom中配置compass监听自动编译
- 71. Simplify Path
- [C++] 回溯法生成数独
- Vaadin 7 和 extjs mvc 集成示例
- android开发练习:天气应用
- 移动端H5页面高清多屏适配方案
- retry policy is RetryUpToMaximumCountWithFixedSleep(maxRetries=10, sleepTime=1 SECONDS)
- mysql日期类型默认值'0000-00-00'容错处理
- Leetcode 38. Count and Say
- 关闭flash的p2p上传,节省网络带宽
- [RxJS] Combining Streams with CombineLatest
- Exceptional C++, 实现一个不区分大小写的字符串比较类
- IOS逆向【1】-SSH
- wifi强度数据采集器(android)
- Unity Diffuse Metal Shader Mod
- [RxJS] Getting Input Text with Map
- [RxJS] Refactoring Composable Streams in RxJS, switchMap()
- MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限 (二)
- MYSQL添加新用户 MYSQL为用户创建数据库 MYSQL为新用户分配权限 (二)