Skip to content

Commit 12b3c5d

Browse files
committed
finish "105. Construct Binary Tree from Preorder and Inorder Traversal". Fix #105
1 parent 5a7dcde commit 12b3c5d

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

Diff for: README.adoc

+6-6
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,12 @@
528528
//|{leetcode_base_url}/maximum-depth-of-binary-tree/[Maximum Depth of Binary Tree]
529529
//|{source_base_url}/MaximumDepthOfBinaryTree.java[Java]
530530
//|Easy
531-
//
532-
//|105
533-
//|{leetcode_base_url}/construct-binary-tree-from-preorder-and-inorder-traversal/[Construct Binary Tree from Preorder and Inorder Traversal]
534-
//|{source_base_url}/ConstructBinaryTreeFromPreorderAndInorderTraversal.java[Java]
535-
//|Medium
536-
//
531+
532+
|105
533+
|{leetcode_base_url}/construct-binary-tree-from-preorder-and-inorder-traversal/[Construct Binary Tree from Preorder and Inorder Traversal]
534+
|{source_base_url}/ConstructBinaryTreeFromPreorderAndInorderTraversal.java[Java]
535+
|Medium
536+
537537
//|106
538538
//|{leetcode_base_url}/construct-binary-tree-from-inorder-and-postorder-traversal/[Construct Binary Tree from Inorder and Postorder Traversal]
539539
//|{source_base_url}/ConstructBinaryTreeFromInorderAndPostorderTraversal.java[Java]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.diguage.algorithm.leetcode;
2+
3+
import com.diguage.algorithm.util.JsonUtils;
4+
import com.diguage.algorithm.util.TreeNode;
5+
6+
/**
7+
* = 105. Construct Binary Tree from Preorder and Inorder Traversal
8+
*
9+
* https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/[Construct Binary Tree from Preorder and Inorder Traversal - LeetCode]
10+
*
11+
* Given preorder and inorder traversal of a tree, construct the binary tree.
12+
*
13+
* *Note:*
14+
*
15+
* You may assume that duplicates do not exist in the tree.
16+
*
17+
* For example, given
18+
*
19+
* ----
20+
* preorder = [3,9,20,15,7]
21+
* inorder = [9,3,15,20,7]
22+
* ----
23+
*
24+
* Return the following binary tree:
25+
*
26+
* ----
27+
* 3
28+
* / \
29+
* 9 20
30+
* / \
31+
* 15 7
32+
* ----
33+
*
34+
* @author D瓜哥, https://www.diguage.com/
35+
* @since 2020-01-04 20:25
36+
*/
37+
public class ConstructBinaryTreeFromPreorderAndInorderTraversal {
38+
/**
39+
* Runtime: 10 ms, faster than 34.14% of Java online submissions for Construct Binary Tree from Preorder and Inorder Traversal.
40+
*
41+
* Memory Usage: 36.1 MB, less than 100.00% of Java online submissions for Construct Binary Tree from Preorder and Inorder Traversal.
42+
*
43+
* Copy from: https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/discuss/34538/My-Accepted-Java-Solution[My Accepted Java Solution - LeetCode Discuss]
44+
*/
45+
public TreeNode buildTree(int[] preorder, int[] inorder) {
46+
return build(preorder, inorder, 0, 0, inorder.length);
47+
}
48+
49+
private TreeNode build(int[] preorder, int[] inorder, int preStart, int inStart, int inEnd) {
50+
if (preStart > preorder.length - 1 || inStart > inEnd) {
51+
return null;
52+
}
53+
int value = preorder[preStart];
54+
int index = inStart;
55+
for (int i = inStart; i <= inEnd; i++) {
56+
if (inorder[i] == value) {
57+
index = i;
58+
break;
59+
}
60+
}
61+
TreeNode root = new TreeNode(value);
62+
root.left = build(preorder, inorder, preStart + 1, inStart, index - 1);
63+
root.right = build(preorder, inorder, preStart + (index - inStart) + 1, index + 1, inEnd);
64+
return root;
65+
}
66+
67+
public static void main(String[] args) {
68+
ConstructBinaryTreeFromPreorderAndInorderTraversal solution = new ConstructBinaryTreeFromPreorderAndInorderTraversal();
69+
int[] preorder = {3, 9, 20, 15, 7};
70+
int[] inorder = {9, 3, 15, 20, 7};
71+
TreeNode tree = solution.buildTree(preorder, inorder);
72+
System.out.println(JsonUtils.toJson(tree));
73+
}
74+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.diguage.algorithm.util;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
6+
/**
7+
* @author D瓜哥, https://www.diguage.com/
8+
* @since 2020-01-04 20:28
9+
*/
10+
public class JsonUtils {
11+
private static final ObjectMapper MAPPER = new ObjectMapper();
12+
13+
public static String toJson(Object object) {
14+
String result = null;
15+
try {
16+
result = MAPPER.writeValueAsString(object);
17+
} catch (JsonProcessingException e) {
18+
e.printStackTrace();
19+
}
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)