zoukankan      html  css  js  c++  java
  • 105. Construct Binary Tree from Preorder and Inorder Traversal

    package LeetCode_105
    /**
     * 105. Construct Binary Tree from Preorder and Inorder Traversal
     * https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/description/
     *
     * 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
     * */
    class Solution {
    
        class TreeNode(var `val`: Int = 0) {
            var left: TreeNode? = null
            var right: TreeNode? = null
        }
    
        /*
        Time complexity:O(n^2), Space complexity:O(n)
        * preorder: root->left->right
        * inorder: left->root->right
        * so the first element of perorder array is the root of the tree,
        * then divide the inorder array based in the the first element of preorder array that two part will be left and right
        * */
        var rootIndex = 0
    
        fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
            rootIndex = 0
            return buildTree(preorder,0,inorder.size-1,inorder)
        }
    
        private fun buildTree(preorder: IntArray, start:Int, end:Int, inorder: IntArray):TreeNode?{
            if (start>end){
                return null
            }
            if (start==end){
                return TreeNode(preorder[rootIndex++])
            }
            val root = TreeNode(preorder[rootIndex++])
            for (i in start..end){
                if (inorder[i]==root.`val`){
                    //if is preorder array, we build left node first
                    //if is postorder array, we build right node first
                    root.left = buildTree(preorder, start, i-1, inorder)
                    root.right = buildTree(preorder, i+1, end, inorder)
                    return root
                }
            }
            return root
        }
    }
  • 相关阅读:
    JS浏览器兼容问题
    jsN位字母数字混合验证码
    js将数字变成数组
    JS跟随鼠标移动的提示框
    Grand Central Dispatch(GCD)编程基础
    C#学习之修饰符
    .NET 开源项目介绍及资源推荐:单元测试
    万般皆LINQ
    .NET 开源项目介绍及资源推荐:IOC容器篇
    Type.GetType(string typeName) returns null !?
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/13258212.html
Copyright © 2011-2022 走看看