-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructBinaryTreefromPreorderandInorderTraversal.java
More file actions
58 lines (47 loc) · 1.72 KB
/
ConstructBinaryTreefromPreorderandInorderTraversal.java
File metadata and controls
58 lines (47 loc) · 1.72 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.leetcode.problems.medium;
import java.util.Arrays;
/**
* @author neeraj on 12/10/19
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class ConstructBinaryTreefromPreorderandInorderTraversal {
public static void main(String[] args) {
TreeNode node = buildTree(new int[]{3, 9, 20, 15, 7}, new int[]{9, 3, 15, 20, 7});
System.out.println(node);
preOrderCounter = 0;
node = buildTree(new int[]{1, 2}, new int[]{1, 2});
System.out.println(node);
node = buildTree(new int[]{-1}, new int[]{-1});
System.out.println(node);
}
static int preOrderCounter = 0;
public static TreeNode buildTree(int[] preorder, int[] inorder) {
preOrderCounter = 0;
return buildTreeUtil(preorder, inorder);
}
private static TreeNode buildTreeUtil(int[] preorder, int[] inorder) {
if (preorder.length == 0 || inorder.length == 0) return null;
TreeNode root = new TreeNode(preorder[preOrderCounter++]);
int indexOfRootIn_Inorder = searchVal(inorder, root.val);
if (indexOfRootIn_Inorder > 0) {
root.left = buildTreeUtil(preorder,
Arrays.copyOfRange(inorder, 0, indexOfRootIn_Inorder));
}
if (indexOfRootIn_Inorder < inorder.length - 1) {
root.right = buildTreeUtil(preorder,
Arrays.copyOfRange(inorder, indexOfRootIn_Inorder + 1, inorder.length));
}
return root;
}
private static int searchVal(int[] arr, int search) {
int counter = 0;
for (int val : arr) {
if (val == search) {
return counter;
}
counter++;
}
return -1;
}
}