【LeetCode】解题105:Construct Binary Tree from Preorder and Inorder Traversal

深入解读ATPG Pattern类型:除了Basic Scan,Clock PO和RAM Sequential模式怎么用? 本文深入解析ATPG高级Pattern类型,包括Clock PO和RAM Sequential模式的应用策略。通过Mentor DFT工具链中的Tessent平台,详细介绍了这些模式的工作原理、配置命令及实战案例,帮助提升芯片测试的故障覆盖率和效率。特别适用于处理时钟域交叉和嵌入式存储器的复杂电路测试需求。 阅读详情

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

Problem 105: Construct Binary Tree from Preorder and Inorder Traversal [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.

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

来源:LeetCode

解题思路

使用前序遍历和中序遍历可以唯一构建一棵二叉树,只需要在中序遍历的数组中找到前序遍历中的根节点,就可以划分左子树和右子树范围,从而递归构建二叉树。

具体过程:

  • 首先构建根节点数值,即preorder[0]:
    r o o t = T r e e N o d e ( p r e o r d e r [ 0 ] ) root = TreeNode(preorder[0]) root=TreeNode(preorder[0])
  • 然后遍历inorder数组,找到inorder[index] = preorder[0],index即为根节点在inorder中的下标。
  • 构建左子树的前序遍历和中序遍历数组:
    l C h i l d p r e = c o p y ( p r e o r d e r [ 1 ] , . . . , p r e o r d e r [ i n d e x ] ) l C h i l d i n = c o p y ( i n o r d e r [ 0 ] , . . . , i n o r d e r [ i n d e x − 1 ] ) lChildpre = copy(preorder[1],...,preorder[index]) \\ lChildin = copy(inorder[0],...,inorder[index-1]) lChildpre=copy(preorder[1],...,preorder[index])lChildin=copy(inorder[0],...,inorder[index1])
  • 同样可以构建右子树相应的数组:
    r C h i l d p r e = c o p y ( p r e o r d e r [ i n d e x + 1 ] , . . . , p r e o r d e r [ e n d − 1 ] ) r C h i l d i n = c o p y ( i n o r d e r [ i n d e x + 1 ] , . . . , i n o r d e r [ e n d − 1 ] ) rChildpre = copy(preorder[index+1],...,preorder[end-1]) \\ rChildin = copy(inorder[index+1],...,inorder[end-1]) rChildpre=copy(preorder[index+1],...,preorder[end1])rChildin=copy(inorder[index+1],...,inorder[end1])
  • 递归构建当前根节点的左右子树:
    r o o t . l e f t = b u i l d T r e e ( l C h i l d p r e , l C h i l d i n ) r o o t . r i g h t = b u i l d T r e e ( r C h i l d p r e , r C h i l d i n ) root.left = buildTree(lChildpre, lChildin) \\ root.right = buildTree(rChildpre, rChildin) root.left=buildTree(lChildpre,lChildin)root.right=buildTree(rChildpre,rChildin)

要点:递归

Solution (Java)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length < 1) return null;
        if(preorder.length == 1) return new TreeNode(preorder[0]);
        
        int index = 0;
        TreeNode root = new TreeNode(preorder[0]);
        for(int i = 0; i < inorder.length; i++){
            if(inorder[i] == preorder[0]){
                index = i;
                break;
            }
        }
        // left Child
        int[] lChildpre = new int[index];
        System.arraycopy(preorder, 1, lChildpre, 0, index);
        int[] lChildin = new int[index];
        System.arraycopy(inorder, 0, lChildin, 0, index);
        root.left = buildTree(lChildpre, lChildin);

        // right Child
        int rlen = preorder.length - index - 1;
        int[] rChildpre = new int[rlen];
        System.arraycopy(preorder, index + 1, rChildpre, 0, rlen);
        int[] rChildin = new int[rlen];
        System.arraycopy(inorder, index + 1, rChildin, 0, rlen);
        root.right = buildTree(rChildpre, rChildin);

        return root;
    }
}
C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告 剑指offer 重建二叉树 参与人数:5246  时间限制:1秒  空间限制:32768K 题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。 分析 阅读详情

相关推荐

【100%通过率】华为od统一考试B卷【需要打开多少监视器】JAVA&Go 实现

某长方形停车场,每个车位上方都有对应监控器,当且仅当在当前车位或者前后左右四个方向任意一个车位范围停车时,监控器才需要打开:给出某一时刻停车场的停车分布,请统计最少需要打开多少个监控器

MISAYAONE的博客 9952

LeetCode 0105. 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,...

liuxiaocs7的博客 157

Matlab实现ICEEMDAN改进的完全自适应噪声集合经验模态分解(完整源码和数据)

1.Matlab实现ICEEMDAN改进的完全自适应噪声集合经验模态分解(完整源码和数据)2.Matlab实现ICEEMDAN改进的完全自适应噪声集合经验模态分解,Matlab完整源码和数据,运行环境matlab2018及以上,运行主程序main即可。3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。4.适用对象:大学生课程设计、期末大作业和毕业设计。5.作者介绍:某大厂资深算法工程师,从事Matlab、Python算法仿真工作8年;擅长智能优化算法、神经网络预测、信号处理、元胞自动机等多种领域的算法仿真实验,更多仿真源码、数据集定制私信+。

C++版-剑指offer 面试题6:重建二叉树(Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal) 解题报告...

剑指offer重建二叉树提交网址:http://www.nowcoder.com/practice/8a19cbe657394eeaac2f6ea9b0f6fcf6?tpId=13&tqId=11157或 leetcode 105:https://leetcode.com/problems/construct-binary-tree-from-pre...

weixin_33801856的博客 654

LeetCode105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ Given preorder and inorder traversa

crazy_jack 8959

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal题解(python)

LeetCode 105: Construct Binary Tree From Preorder and Inorder Traversal 分类:Tree & Recursion 难度:M 描述:给了一个树的先序遍历和中序遍历的结果,让把这歌二叉树给复现出来。 preorder = [3,9,20,15,7] inorder = [9,3,15,20,7] Tree: 3 / \ 9 20 / \ 15 7 链接: Construct Binary T

Bourns_A的博客 371

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 that duplicates do not exist in the tree.  原题链接:https://leetcode.com/problems/construct-binary-tree

WangT443的博客 722

Leetcode105. Construct Binary Tree from Preorder and Inorder Traversal

Leetcode105. 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...

magic_jiayu的博客 286

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal (python+cpp)

Leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal题目解析 题目 解析 观察前序遍历和中序遍历可以发现,前序遍历的第一个节点是根节点,从中序遍历中找到这个节点的位置,这个节点左边是左子树,右遍是右子树。根据这样的规律,我们可以用前序遍历的顺序作为索引,将中序遍历进行不断切分来构造树。递归函数的详情如下: 新建一个全局变量preindex,代表在前序遍历中的位置,在递归过程中每次递归这个变量加1 递归函数接受两个参

qq_37821701的博客 406

LeetCode-105. Construct Binary Tree from Preorder and Inorder Traversal [C++][Java]

Given two integer arrayspreorderandinorderwherepreorderis the preorder traversal of a binary tree andinorderis the inorder traversal of the same tree, construct and returnthe binary tree.

贫道绝缘子的博客 496

LeetCode 105 Construct Binary Tree from Preorder and Inorder Traversal-前序中序遍历构造二叉树-Python和Java递归

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 that duplicates do not e...

个人博客 923

LeetCode解题126:Word Ladder II(BFS算法

LeetCode解题 126:Word Ladder II(BFS算法)Problem 126: Word Ladder II [Hard]解题思路Solution (Java)修改过程 Problem 126: Word Ladder II [Hard] Given two words (beginWord and endWord), and a dictionary’s word list, ...

Fayedily的博客 771

LeetCode解题188:Best Time to Buy and Sell Stock IV(动态规划)

LeetCode解题 188:Best Time to Buy and Sell Stock IV (动态规划)Problem 188: Best Time to Buy and Sell Stock IV [Hard]解题思路Solution (Java)修改过程 Problem 188: Best Time to Buy and Sell Stock IV [Hard] Say you hav...

Fayedily的博客 594

LeetCode解题123:Best Time to Buy and Sell Stock III(动态规划)

LeetCode解题 123:Best Time to Buy and Sell Stock III (动态规划)Problem 123: Best Time to Buy and Sell Stock III [Hard]解题思路I. O(2n)时间+O(3n)空间II. O(2n)时间+O(n)空间III. O(n)时间+O(4)空间Solution (Java) Problem 123: B...

Fayedily的博客 487

LeetCode解题119:Pascal's Triangle II

LeetCode解题 118:Pascal's Triangle II Problem 118: Pascal's Triangle II [Easy]解题思路Solution (Java) Problem 118: Pascal’s Triangle II [Easy] Given a non-negative index k where k ≤ 33, return the kth index...

Fayedily的博客 444

LeetCode解题78:Subsets

LeetCode解题 78:Subsets Problem 78: Subsets [Medium]解题思路Solution (Java)修改过程 Problem 78: Subsets [Medium] Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The so...

Fayedily的博客 443

LeetCode解题44:Wildcard Matching(动态规划解

LeetCode解题 44:Wildcard Matching(动态规划解)Problem 44: Wildcard Matching [Hard]解题思路Solution (Java) Problem 44: Wildcard Matching [Hard] Given an input string (s) and a pattern (p), implement wildcard patt...

Fayedily的博客 429

LeetCode解题10:Regular Expression Matching(动态规划解

LeetCode解题 10:Regular Expression Matching(动态规划解)Problem 10: Regular Expression Matching [Hard]解题思路Solution (Java) Problem 10: Regular Expression Matching [Hard] Given an input string (s) and a patter...

Fayedily的博客 411

LeetCode解题122:Best Time to Buy and Sell Stock II

LeetCode解题 122:Best Time to Buy and Sell Stock II Problem 122: Best Time to Buy and Sell Stock II [Easy]解题思路Solution (Java) Problem 122: Best Time to Buy and Sell Stock II [Easy] Say you have an array...

Fayedily的博客 365

LeetCode解题32:Longest Valid Parentheses(多解:栈+动态规划+计数器)

LeetCode解题 32:Longest Valid Parentheses(多解:栈+动态规划+计数器)Problem 32: Longest Valid Parentheses [Hard]解题思路1. 栈2. 动态规划3. 计数器Solution (Java) Problem 32: Longest Valid Parentheses [Hard] Given a string cont...

Fayedily的博客 352

LeetCode解题31:Next Permutation

LeetCode解题 31:Next Permutation Problem 31: Next Permutation [Medium]解题思路Solution (Java)修改过程 Problem 31: Next Permutation [Medium] Implement next permutation, which rearranges numbers into the lexicogr...

Fayedily的博客 351
上一篇: 【LeetCode】解题90:Subsets II
下一篇: 【LeetCode】解题118:Pascal's Triangle
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值