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 the tree.

解题思路分析:

前序遍历首先遍历根节点,然后依次遍历左右子树

中序遍历首先遍历左子树,然后遍历根结点,最后遍历右子树

根据二者的特点,前序遍历的首节点就是根结点,然后在中序序列中找出该结点位置(index),则该结点之前的为左子树结点,该节点之后为右子树结点;

同样对于前序序列,首结点之后的index个结点为左子树结点,剩余的节点为右子树结点;

最后,递归建立子树即可。

java代码:

 1 public class Solution {
 2     public TreeNode buildTree(int[] preorder, int[] inorder) {
 3         if(preorder == null || preorder.length == 0){
 4             return null;
 5         }
 6
 7         int flag = preorder[0];
 8         TreeNode root = new TreeNode(flag);
 9         int i = 0;
10         for(i=0; i<inorder.length; i++){
11             if(flag == inorder[i]){
12                 break;
13             }
14         }
15
16         int[] pre_left, pre_right, in_left, in_right;
17         pre_left = new int[i];
18         for(int j=1; j<=i;j++){
19             pre_left[j-1] = preorder[j];
20         }
21         pre_right = new int[preorder.length-i-1];
22         for(int j=i+1; j<preorder.length;j++){
23             pre_right[j-i-1] = preorder[j];
24         }
25
26         in_left = new int[i];
27         for(int j=0;j<i;j++){
28             in_left[j] = inorder[j];
29         }
30         in_right = new int[inorder.length-i-1];
31         for(int j=i+1; j<inorder.length; j++){
32             in_right[j-i-1] = inorder[j];
33         }
34         root.left = buildTree(pre_left,in_left);
35         root.right = buildTree(pre_right,in_right);
36
37
38
39         return root;
40
41     }
42
43
44 }
时间: 2024-08-01 04:36:24

leetcode105:Construct Binary Tree from Preorder and Inorder Traversal的相关文章

LeetCode:Construct Binary Tree from Preorder and Inorder Traversal

要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode *left; 4 TreeNode *right; 5 TreeNode(int x): val(x),left(NULL), right(NULL) {} 6 }; 7 8 typedef vector<int>::iterator Iter; 9 TreeNode *buildTree(vector<int> &preord

[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. 思路 主要是根据前序遍历和中序遍历的特点解决这个题目. 1.确定树的根节点.树根是当前树中所有元素在前序遍历中最先出现的元素. 2.求解树的子树.找出根节点在中序遍历中的位置,根左边的所有元素就是左子树,根右边的所有元

43: Construct Binary Tree from Preorder and Inorder Traversal

/************************************************************************/            /*       43:  Construct Binary Tree from Preorder and Inorder Traversal                            */            /**************************************************

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

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. SOLUTION 1: 1. Find the root node from the preorder.(it

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. 分析:通过一个二叉树的先序遍历和中序遍历可以确定一个二叉树.先序遍历的第一个元素对应二叉树的根结点,由于在中序遍历中左子树和右子树的中序遍历序列分别在根节点两侧,因此我们可以确定左子树和右子树的中序遍历序列.在先序遍历序列中,

【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 duplicates do not exist in the tree. 类似http://www.cnblogs.com/sunshineatnoon/p/3854935.html 只是子树的前序和中序遍历序列分别更新为: //左子树: left_prestart = prestart+1 lef

[leetcode]Construct Binary Tree from Preorder and Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 题意:根据二叉树的先序遍历和中序遍历恢复二叉树. 解题思路:可以参照 http://www.cnblogs.com/zuoyuan/p/3720138.html 的思路.递归进行解决. 代码: # Definition for a binary tree node # class TreeNode: # d

36. Construct Binary Tree from Inorder and Postorder Traversal &amp;&amp; Construct Binary Tree from Preorder and Inorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal OJ: https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assu

Construct Binary Tree from Preorder and Inorder Traversal leetcode java

题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 题解: 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为了清晰表示