在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

[LintCode/LeetCode] Construct Binary Tree from Traversal

2016-8-29 13:24| 发布者: zhangjf| 查看: 651| 评论: 0

摘要: Construct Binary Tree from Inorder and Preorder Traversal Problem Given preorder and inorder traversal of a tree, construct the binary tree. Notice You may assume that duplicates do not exist in the t ...
Construct Binary Tree from Inorder and Preorder Traversal Problem

Given preorder and inorder traversal of a tree, construct the binary tree.

Notice

You may assume that duplicates do not exist in the tree.

Example

Given in-order [1,2,3] and pre-order [2,1,3], return a tree:

  1. 2
  2. /
  3. 1 3
复制代码
Note

许久未更。做了几道二分法的题目练手,发现这道题已经淡忘了,记录一下。

这道题目的要点在于找inorder的区间。preStart每增加一次,就对应一个新的子树。每个子树的根节点都可以在inorder的中间某处找到,以此为界,左边是这个根节点的左子树,右边是右子树。不断递归,得解。

边界条件需要注意:

若preorder或inorder数组为空,返回空;

当preStart前进到超出preorder末位,或inStart超过inEnd,返回空;

每次创建完根节点之后,要将preStart加1,才能进行递归。

Solution
  1. public class Solution {
  2. int preStart = 0;
  3. public TreeNode buildTree(int[] preorder, int[] inorder) {
  4. if (preorder.length == 0 || inorder.length == 0) return null;
  5. return helper(preorder, inorder, 0, preorder.length-1);
  6. }
  7. public TreeNode helper(int[] preorder, int[] inorder, int inStart, int inEnd) {
  8. if (preStart >= preorder.length || inStart > inEnd) return null;
  9. int index = 0;
  10. for (int i = inStart; i <= inEnd; i++) {
  11. if (inorder[i] == preorder[preStart]) {
  12. index = i;
  13. break;
  14. }
  15. }
  16. TreeNode root = new TreeNode(preorder[preStart++]);
  17. root.left = helper(preorder, inorder, inStart, index-1);
  18. root.right = helper(preorder, inorder, index+1, inEnd);
  19. return root;
  20. }
  21. }
复制代码
Construct Binary Tree from Inorder and Postorder Traversal Problem

Given inorder and postorder traversal of a tree, construct the binary tree.

Notice

You may assume that duplicates do not exist in the tree.

Example

Given inorder [1,2,3] and postorder [1,3,2], return a tree:

  1. 2
  2. /
  3. 1 3
复制代码
Note

和先序+中序方法一样,不过这次是逆推,递归的时候先右子树,后左子树即可。

Solution Recursion
  1. public class Solution {
  2. int postEnd;
  3. public TreeNode buildTree(int[] inorder, int[] postorder) {
  4. postEnd = postorder.length - 1;
  5. return helper(postorder, inorder, 0, inorder.length - 1);
  6. }
  7. private TreeNode helper(int[] postorder, int[] inorder, int inStart, int inEnd) {
  8. if (postEnd < 0 || inStart > inEnd) return null;
  9. TreeNode root = new TreeNode(postorder[postEnd--]);
  10. int inMid = 0;
  11. for (int i = inStart; i <= inEnd; i++) {
  12. if (inorder[i] == root.val) {
  13. inMid = i;
  14. break;
  15. }
  16. }
  17. root.right = helper(postorder, inorder, inMid +1, inEnd);
  18. root.left = helper(postorder, inorder, inStart, inMid-1);
  19. return root;
  20. }
  21. }
复制代码
Using Stack
  1. public class Solution {
  2. public TreeNode buildTree(int[] inorder, int[] postorder) {
  3. if (inorder == null || inorder.length < 1) return null;
  4. int i = inorder.length - 1;
  5. int p = i;
  6. TreeNode node;
  7. TreeNode root = new TreeNode(postorder[postorder.length - 1]);
  8. Stack<TreeNode> stack = new Stack<>();
  9. stack.push(root);
  10. p--;
  11. while (true) {
  12. if (inorder[i] == stack.peek().val) { // inorder[i] is on top of stack, pop stack to get its parent to get to left side
  13. if (--i < 0) break;
  14. node = stack.pop();
  15. if (!stack.isEmpty() && inorder[i] == stack.peek().val) continue;
  16. node.left = new TreeNode(postorder[p]);
  17. stack.push(node.left);
  18. } else { // inorder[i] is not on top of stack, postorder[p] must be right child
  19. node = new TreeNode(postorder[p]);
  20. stack.peek().right = node;
  21. stack.push(node);
  22. }
  23. p--;
  24. }
  25. return root;
  26. }
  27. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-9-14 00:15

Copyright 2015-2025 djqfx

返回顶部