Skip to content

Commit 7f43cfa

Browse files
author
liwentian
committed
construct binary tree from preorder and inorder traversal
1 parent 563797d commit 7f43cfa

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎doc/Leetcodes.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
|101|[Symmetric Tree](https://leetcode.com/problems/symmetric-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/SymmetricTree.java)|95|
5151
|102|[Binary Tree Level Order Traversal](https://leetcode.com/problems/binary-tree-level-order-traversal/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BinaryTreeLevelOrderTraversal.java)|85|
5252
|104|[Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MaximumDepthOfBinaryTree.java)|100|
53+
|105|[Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ConstructBinaryTreeFromPreorderInorderTraversal.java)|95|
5354
|108|[Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/ConvertSortedArrayToBinarySearchTree.java)|100|
5455
|110|[Balanced Binary Tree](https://leetcode.com/problems/balanced-binary-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/BalancedBinaryTree.java)|80|
5556
|111|[Minimum Depth of Binary Tree](https://leetcode.com/problems/minimum-depth-of-binary-tree/)| [Java](https://github.com/dingjikerbo/leetcode/blob/master/solution/src/main/java/com/inuker/solution/MinimumDepthOfBinaryTree.java)|80|
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.inuker.solution;
2+
3+
/**
4+
* Created by dingjikerbo on 16/11/30.
5+
*/
6+
7+
public class ConstructBinaryTreeFromPreorderInorderTraversal {
8+
9+
public TreeNode buildTree(int[] preorder, int[] inorder) {
10+
return buildTree(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
11+
}
12+
13+
private TreeNode buildTree(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) {
14+
if (preStart > preEnd) {
15+
return null;
16+
}
17+
18+
int index;
19+
for (index = inStart; index <= inEnd; index++) {
20+
if (inorder[index] == preorder[preStart]) {
21+
break;
22+
}
23+
}
24+
25+
int len = index - inStart;
26+
TreeNode root = new TreeNode(preorder[preStart]);
27+
root.left = buildTree(preorder, preStart + 1, preStart + len, inorder, inStart, index - 1);
28+
root.right = buildTree(preorder, preStart + len + 1, preEnd, inorder, index + 1, inEnd);
29+
return root;
30+
}
31+
}

0 commit comments

Comments
 (0)