leetcode Construct Binary Tree from Preorder and Inorder Traversal

原创 2016年06月01日 17:51:48

题目链接

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
      public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(inorder==null||inorder.length==0)
        {
            return null;
        }


        TreeNode root=new TreeNode(preorder[0]);
        build(root, inorder, 0, inorder.length-1, preorder, 0, preorder.length-1);

        return root;
    }


    public void build(TreeNode root,int[] inorder, int ins,int ine,int[] preorder,int pres,int pree)
    {
        int parentIndexInMiddle=ins;

        while(inorder[parentIndexInMiddle]!=root.val)
        {
            parentIndexInMiddle++;
        }

        int leftCount=parentIndexInMiddle-ins;



        if(leftCount>0)
        {
            int leftRootIndexInPre=pres+1;
            root.left=new TreeNode(preorder[leftRootIndexInPre]);
            build(root.left, inorder, ins, parentIndexInMiddle-1, preorder, leftRootIndexInPre, pres+1+leftCount-1);
        }

        int rightCount=ine-parentIndexInMiddle;
        if(rightCount>0)
        {
            int rightRootIndexInpre=pree-rightCount+1;
            root.right=new TreeNode(preorder[rightRootIndexInpre]);
            build(root.right, inorder, parentIndexInMiddle+1, ine, preorder, rightRootIndexInpre, pree);
        }
    }
}

leetcode No105. Construct Binary Tree from Preorder and Inorder Traversal

Question: Given preorder and inorder traversal of a tree, construct the binary tree. 根据树的前序遍历和中序遍历...

【leetcode】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序、中序、后序都是针对于根结点而言,所以又叫作先根、中根、后根(当然不是高跟)。 前序:根  左 右 中序:左  根 右 对二叉树,我们...

LeetCode题解-105-Construct Binary Tree from Preorder and Inorder Traversal

原题 Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume ...

Leetcode: Construct Binary Tree from Preorder and Inorder Traversal

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume ...

[leetcode]105. Construct Binary Tree from Preorder and Inorder Traversal@Java解题报告

https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/...

leetcode之Construct Binary Tree from Preorder and Inorder Traversal 问题

问题描述: Given preorder and inorder traversal of a tree, construct the binary tree.(意思很简单,给出先序和中序遍历的序列...

【LeetCode笔记】Construct Binary Tree from Preorder and Inorder Traversal 前序、中序还原二叉树

思路: 1.这道题还是借鉴了其他大佬的思路,仔细分析一下,前序遍历的第一个值是根节点值(假设为val),那么在中序遍历中val之前的都是根节点的左子树,val值之后的都是右子树。同理递归可还原二叉树...
  • macidoo
  • macidoo
  • 2017年04月11日 21:49
  • 86

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal 先序和中序中恢复二叉树 解题报告

1 解题思想已知二叉树的先序和中序序列,如何恢复得到一个完整的二叉树?首先说说先序,中序,后序,其实就是按照访问到根节点的顺序来命名的先序:中左右 中序:左中右 后序:左右中好了,这里我们已知先序...
  • MebiuW
  • MebiuW
  • 2016年09月16日 16:33
  • 385

leetcode:二叉树之Construct Binary Tree from Preorder and Inorder Traversal

leetcode:二叉树之Construct Binary Tree from Preorder and Inorder Traversal 题目: Given preorder and inorde...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Construct Binary Tree from Preorder and Inorder Traversal
举报原因:
原因补充:

(最多只允许输入30个字)