LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal

不断递归的实现!!!!

下面是AC代码:


 1 /**
2 * Given inorder and postorder traversal of a tree, construct the binary tree.
3 * @param inorder
4 * @param postorder
5 * @return
6 */
7 public TreeNode buildTree(int[] inorder,int[] postorder){
8 if(inorder == null || postorder ==null
9 || inorder.length == 0 || postorder.length == 0)
10 return null;
11 if(inorder.length!=postorder.length)
12 return null;
13 if(inorder.length == 1)
14 {
15 if(inorder[0] != postorder[0])
16 return null;
17 else
18 return new TreeNode(inorder[0]);
19 }
20 // make sure how many nodes in the left
21 int i=0;
22 while(inorder[i++] != postorder[postorder.length-1]);
23 int[] leftInorder = new int[i-1];
24 int[] leftPostorder = new int[i-1];
25 int j = 0;
26 while(j<i-1){
27 leftInorder[j] = inorder[j];
28 leftPostorder[j] = postorder[j];
29 j++;
30 }
31 TreeNode left = buildTree(leftInorder, leftPostorder);
32 TreeNode root = new TreeNode(postorder[postorder.length-1]);
33 root.left = left;
34 // find the right subtree nodes
35 int[] rightInorder = new int[inorder.length - (i-1) -1];
36 int[] rightPostorder = new int[postorder.length - (i-1) -1];
37
38 j = i;
39 while(j<inorder.length){
40 rightInorder[j-i] = inorder[j];
41 rightPostorder[j-i] = postorder[j-1];
42 j++;
43 }
44 root.right = buildTree(rightInorder,rightPostorder);
45
46 return root;
47 }
48 /**
49 * Given preorder and inorder traversal of a tree,
50 * construct the binary tree.
51 * @param preorder
52 * @param inorder
53 * @return
54 */
55 public TreeNode buildTree2(int[] preorder, int[] inorder){
56 if(preorder == null || inorder == null ||
57 preorder.length == 0 || inorder.length == 0)
58 return null;
59 if(preorder.length != inorder.length)
60 return null;
61 int len = 0;
62 while(inorder[len++]!=preorder[0]);
63 //determine the left subtree
64 int[] leftInorder = new int[len-1];
65 int[] leftPreorder = new int[len-1];
66
67 int j =0;
68 while(j<len-1)
69 {
70 leftInorder[j] = inorder[j];
71 leftPreorder[j] = preorder[j+1];
72 j++;
73 }
74
75 TreeNode left = buildTree( leftPreorder,leftInorder);
76 TreeNode root = new TreeNode(preorder[0]);
77 root.left =left;
78 //determine the right subtree
79 int[] rightInorder = new int[inorder.length - (len-1) -1];
80 int[] rightPreorder = new int[preorder.length - (len-1) -1];
81 j = len;
82 while(j<inorder.length){
83 rightInorder[j-len] = inorder[j];
84 rightPreorder[j-len] = preorder[j];
85 j++;
86 }
87 root.right = buildTree(rightPreorder,rightInorder);
88 return root;
89 }

LeetCode OJ - construct Binary Tree from Inorder and
Postorder/Preorder Traversal,布布扣,bubuko.com

LeetCode OJ - construct Binary Tree from Inorder and
Postorder/Preorder Traversal

时间: 2024-12-25 07:30:13

LeetCode OJ - construct Binary Tree from Inorder and Postorder/Preorder Traversal的相关文章

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

LeetCode | 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树【Python】

LeetCode 0106. Construct Binary Tree from Inorder and Postorder Traversal从中序与后序遍历序列构造二叉树[Medium][Python][二叉树][递归] Problem LeetCode Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

106. Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 60461 Total Submissions: 203546 Difficulty: Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not

Leetcode dfs Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 14363 Total Submissions: 54254My Submissions Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in t

Java for LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Inorder and Postorder Traversal Total Accepted: 31041 Total Submissions: 115870 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 解题思路

leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal &amp;amp; Construct Binary Tree f

1.  Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 代码: class Solution { public: TreeNode *buildTr

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 这个题目是给你一棵树的中序遍历和后序遍历,让你将这棵树表示出来.其中可以假设在树中没有重复的元素. 当做完这个题之后,建议去做做第105题,跟这道题类似. 分析:这个解法的基本思想是:我们有两个数组,分别是IN和POST.后

leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod