Skip to content

Commit c9e88e3

Browse files
authored
Create Ostrichcrab.md
1 parent 35995df commit c9e88e3

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

2018.12.08-leetcode105/Ostrichcrab.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public TreeNode buildTreeHelper(int[] inorder, int is, int ie, int[] preorder, int ps){
13+
if(is>ie || ps>=preorder.length) return null;
14+
TreeNode root = new TreeNode(preorder[ps]);
15+
int rootAtIn = is;
16+
while(inorder[rootAtIn] != root.val) rootAtIn++;
17+
root.left = buildTreeHelper(inorder,is,rootAtIn-1,preorder,ps+1);
18+
root.right = buildTreeHelper(inorder,rootAtIn+1,ie,preorder,ps+rootAtIn-is+1);
19+
return root;
20+
}
21+
public TreeNode buildTree(int[] preorder, int[] inorder) {
22+
return buildTreeHelper(inorder,0,inorder.length-1,preorder,0);
23+
}
24+
}
25+
```

0 commit comments

Comments
 (0)