forked from leetcoders/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstructBinaryTreefromPreorderandInorderTraversal.java
43 lines (41 loc) · 1.43 KB
/
ConstructBinaryTreefromPreorderandInorderTraversal.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
/*
Author: Andy, [email protected]
Date: Jan 16, 2015
Problem: Construct Binary Tree from Preorder and Inorder Traversal
Difficulty: Medium
Source: https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
Notes:
Given preorder and inorder traversal of a tree, construct the binary tree.
Note:
You may assume that duplicates do not exist in the tree.
Solution: Recursion.
*/
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
if(inorder.length == 0 || preorder.length == 0 || inorder.length != preorder.length) return null;
return buildTreeRe(preorder, 0, inorder, 0, preorder.length);
}
public TreeNode buildTreeRe(int[] preorder, int s1, int[] inorder, int s2, int size) {
if (size <= 0 ) return null;
TreeNode node = new TreeNode(preorder[s1]);
if (size == 1) return node;
int pos = s2;
while (pos <= (s2 + size - 1)) {
if (inorder[pos] == preorder[s1]) break;
++pos;
}
int leftlen = pos - s2;
node.left = buildTreeRe(preorder, s1 + 1, inorder, s2, leftlen);
node.right = buildTreeRe(preorder, s1 + leftlen + 1, inorder, pos + 1, size - leftlen - 1);
return node;
}
}