zoukankan      html  css  js  c++  java
  • 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 that duplicates do not exist in the tree.

    For example, given

    preorder = [3,9,20,15,7]
    inorder = [9,3,15,20,7]

    Return the following binary tree:

        3
       / 
      9  20
        /  
       15   7
    public TreeNode buildTree(int[] preorder, int[] inorder) {
             int preLength = preorder.length;
             int inLength = inorder.length;
            return buildTree(preorder, 0, preLength-1, inorder, 0, inLength-1);
         }
       
        public TreeNode buildTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
             if(inStart > inEnd || preStart > preEnd)
                 return null;
                 
            int rootVal = pre[preStart];
            int rootIndex = 0;
            for(int i = inStart; i <= inEnd; i++){
                 if(in[i] == rootVal){
                     rootIndex = i;
                     break;
                 }
             }
           
             int len = rootIndex - inStart;
             TreeNode root = new TreeNode(rootVal);
             root.left = buildTree(pre, preStart+1, preStart+len, in, inStart, rootIndex-1);
             root.right = buildTree(pre, preStart+len+1, preEnd, in, rootIndex+1, inEnd);
           
            return root;
         }

    题解

         1       
        /    
       2   3   
      /  /    
     4  5 6  7

    对于上图的树来说,
    index: 0 1 2 3 4 5 6
    先序遍历为: 1 2 4 5 3 6 7
    中序遍历为: 4 2 5 1 6 3 7
    为了清晰表示,我给节点上了颜色,红色是根节点,蓝色为左子树,绿色为右子树。
    可以发现的规律是:
    1. 先序遍历的从左数第一个为整棵树的根节点。
    2. 中序遍历中根节点是左子树右子树的分割点。


    再看这个树的左子树:
    先序遍历为: 2 4 5
    中序遍历为: 4 2 5
    依然可以套用上面发现的规律。

    右子树:
    先序遍历为: 3 6 7
    中序遍历为: 6 3 7
    也是可以套用上面的规律的。

    所以这道题可以用递归的方法解决。
    具体解决方法是:
    通过先序遍历找到第一个点作为根节点,在中序遍历中找到根节点并记录index。
    因为中序遍历中根节点左边为左子树,所以可以记录左子树的长度并在先序遍历中依据这个长度找到左子树的区间,用同样方法可以找到右子树的区间。
    递归的建立好左子树和右子树就好。

    代码如下:
    public TreeNode buildTree(int[] preorder, int[] inorder) {
             int preLength = preorder.length;
             int inLength = inorder.length;
            return buildTree(preorder, 0, preLength-1, inorder, 0, inLength-1);
         }
       
        public TreeNode buildTree(int[] pre, int preStart, int preEnd, int[] in, int inStart, int inEnd){
             if(inStart > inEnd || preStart > preEnd)
                 return null;
                 
            int rootVal = pre[preStart];
            int rootIndex = 0;
            for(int i = inStart; i <= inEnd; i++){
                 if(in[i] == rootVal){
                     rootIndex = i;
                     break;
                 }
             }
           
             int len = rootIndex - inStart;
             TreeNode root = new TreeNode(rootVal);
             root.left = buildTree(pre, preStart+1, preStart+len, in, inStart, rootIndex-1);
             root.right = buildTree(pre, preStart+len+1, preEnd, in, rootIndex+1, inEnd);
           
            return root;
         }

    以上转自 https://www.cnblogs.com/springfor/p/3884034.html

  • 相关阅读:
    c# webapi无法获取Session值问题解决
    深入理解java虚拟机之自动内存管理机制笔记
    数据结构总结1
    疯人院之语言、编码、计算机

    集线器/交换机
    什么是DOM?DOM和JavaScript的关系 [web开发]
    JSON轻量级的数据交换格式
    天问宇宙学第一课
    C++基础知识
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/10504054.html
Copyright © 2011-2022 走看看