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

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

public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(preorder.length==0||inorder.length==0)
return null;
return BuildTreeNode(preorder,0,preorder.length-1,inorder,0,inorder.length-1); } private TreeNode BuildTreeNode(int[] preorder, int prestart, int preend, int[] inorder,
int instart, int inend) {
if(prestart>preend||instart>inend)
return null;
int temp = preorder[prestart];
TreeNode root = new TreeNode(temp);
int leftlength=0; for(int i=instart;i<=inend;i++){
if(inorder[i]!=temp)
leftlength++;
else if(inorder[i]==temp)
break;
}
root.left=BuildTreeNode(preorder, prestart+1, prestart+leftlength, inorder, instart,instart+leftlength-1);
root.right=BuildTreeNode(preorder,prestart+leftlength+1,preend,inorder,instart+leftlength+1,inend);
return root;
}
}

【LeetCode】Construct Binary Tree from Preorder and Inorder Traversal的更多相关文章

  1. 【Leetcode】【Medium】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 ...

  2. 【树】Construct Binary Tree from Preorder and Inorder Traversal

    题目: Given preorder and inorder traversal of a tree, construct the binary tree. 思路: 线序序列的第一个元素就是树根,然后 ...

  3. 【LeetCode105】Construct Binary Tree from Preorder and Inorder Traversal★★

    1.题目 2.思路 3.java代码 //测试 public class BuildTreeUsingInorderAndPreorder { public static void main(Stri ...

  4. [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 ...

  5. (二叉树 递归) 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 ...

  6. 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal

    Problem Link: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-trave ...

  7. 【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 that ...

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

  9. leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal ----- java

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

随机推荐

  1. STL之set的用法

    1.关于set 首先,set是关联容器,set作为一个容器是用来存储同一种数据类型的数据结构,基本功能与数组相似.不同的是,在set中每个元素的值都是唯一的.而且系统能够根据元素的值自动进行排序.但是 ...

  2. Codeforces 868F Yet Another Minimization Problem(分治+莫队优化DP)

    题目链接  Yet Another Minimization Problem 题意  给定一个序列,现在要把这个序列分成k个连续的连续子序列.求每个连续子序列价值和的最小值. 设$f[i][j]$为前 ...

  3. 某考试 T1 Hello my friend

    Discription

  4. java poi excel 生成表格的工具封装

    效果如下: 代码如下: import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import ...

  5. Leetcode总结之Tree

    package Tree; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; imp ...

  6. 用AntRun插件测试Maven的生命周期

    在用AntRun插件之前,需要了解以下几个知识点: 1.Maven的生命周期,参考:http://www.cnblogs.com/EasonJim/p/6816340.html,主要是要知道生命周期里 ...

  7. Node之父ry发布新项目deno:下一代Node

    https://mp.weixin.qq.com/s/1LcO3EqGV2iRlZ1aIrQeqw

  8. iOS经常使用设计模式——单例模式

    第一部分: 创建一个单例对象 单例的应用场景: 单例模式用于当一个类仅仅能有一个实例的时候. 通常情况下这个"单例"代表的是某一个物理设备比方打印机,或是某种不能够有多个实例同一时 ...

  9. 解决Gradle执行命令时报Could not determine the dependencies of task &#39;:compileReleaseJava&#39;.

    Could not determine the dependencies of task ':compileReleaseJava'. > failed to find target andro ...

  10. Surrounded Regions 包围区域——dfs

    Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...