-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_106.java
44 lines (37 loc) · 1.29 KB
/
LeetCode_106.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class LeetCode_106 {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return dfs(inorder, postorder, postorder.length - 1, 0, inorder.length - 1);
}
public TreeNode dfs(int[] inorder, int[] postorder, int postEnd, int inStart, int inEnd) {
if(inStart > inEnd) {
return null;
}
TreeNode node = new TreeNode(postorder[postEnd]);
// int index = list.indexOf(node.val);
int index = 0;
for (; index < inEnd; index++) {
if(inorder[index] == node.val)
break;
}
node.left = dfs(inorder, postorder, postEnd - (inEnd - index + 1), inStart, index - 1);
node.right = dfs(inorder, postorder, postEnd - 1, index + 1, inEnd);
return node;
}
}
}