Skip to content

Commit 03b42bf

Browse files
committed
Added solution for tree build
1 parent b358efa commit 03b42bf

File tree

6 files changed

+226
-3
lines changed

6 files changed

+226
-3
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.eprogrammerz.examples.algorithm.trees;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Given inorder and postorder traversal of a tree, construct the binary tree.
12+
*
13+
* Note: You may assume that duplicates do not exist in the tree.
14+
*
15+
* Input :
16+
* Inorder : [2, 1, 3]
17+
* Postorder : [2, 3, 1]
18+
*
19+
* Return :
20+
* 1
21+
* / \
22+
* 2 3
23+
*/
24+
public class BinaryTreeFromInorderAndPost {
25+
/**
26+
* The idea is that the last element in post order traversal will be the root!
27+
* So, find index in inorder traversal, which left is left of root
28+
* and right is right of the root
29+
*
30+
* @param inOrder
31+
* @param post
32+
* @return
33+
*/
34+
public TreeNode buildTree(List<Integer> inOrder, List<Integer> post) {
35+
if (inOrder == null || post == null || inOrder.size() != post.size() || inOrder.isEmpty()) return null;
36+
37+
int rootVal = post.get(post.size() - 1);
38+
TreeNode root = new TreeNode(rootVal);
39+
40+
int idx = getRootIdx(inOrder,rootVal);
41+
42+
root.left = buildTree(inOrder.subList(0, idx), post.subList(0, idx));
43+
root.right = buildTree(inOrder.subList(idx + 1, inOrder.size()), post.subList(idx, post.size() - 1));
44+
45+
return root;
46+
}
47+
48+
private int getRootIdx(List<Integer> inOrder, int rootVal) {
49+
return inOrder.indexOf(rootVal);
50+
}
51+
52+
@Test
53+
public void testBuildTree() {
54+
List<Integer> inOrder = Arrays.asList(2,1,3,4,5);
55+
List<Integer> post = Arrays.asList(2,3,1,5,4);
56+
57+
TreeNode tree = buildTree(inOrder, post);
58+
assertEquals(4, tree.val);
59+
assertEquals(1, tree.left.val);
60+
assertEquals(2, tree.left.left.val);
61+
assertEquals(3, tree.left.right.val);
62+
assertEquals(5, tree.right.val);
63+
}
64+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.eprogrammerz.examples.algorithm.trees;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Given inorder and preorder traversal of a tree, construct the binary tree.
12+
*
13+
* Note: You may assume that duplicates do not exist in the tree.
14+
*
15+
* Input :
16+
* Inorder : [2, 1, 3]
17+
* preorder : [1, 2, 3]
18+
*
19+
* Return :
20+
* 1
21+
* / \
22+
* 2 3
23+
*/
24+
public class BinaryTreeFromInorderAndPreorder {
25+
public TreeNode buildTree(List<Integer> inOrder, List<Integer> pre) {
26+
if (inOrder == null || pre == null || inOrder.size() != pre.size() || inOrder.isEmpty()) return null;
27+
28+
int rootVal = pre.get(0);
29+
TreeNode root = new TreeNode(rootVal);
30+
31+
int idx = inOrder.indexOf(rootVal);
32+
root.left = buildTree(inOrder.subList(0, idx), pre.subList(1, idx + 1));
33+
root.right = buildTree(inOrder.subList(idx + 1, inOrder.size()), pre.subList(idx + 1, pre.size()));
34+
return root;
35+
}
36+
37+
@Test
38+
public void testBuildTree() {
39+
List<Integer> inOrder = Arrays.asList(2,1,3,4,5);
40+
List<Integer> pre = Arrays.asList(4,1,2,3,5);
41+
42+
TreeNode tree = buildTree(inOrder, pre);
43+
assertEquals(4, tree.val);
44+
assertEquals(1, tree.left.val);
45+
assertEquals(2, tree.left.left.val);
46+
assertEquals(3, tree.left.right.val);
47+
assertEquals(5, tree.right.val);
48+
}
49+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.eprogrammerz.examples.algorithm.trees;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Given BST and k, 1 <= k <= total element in BST
12+
* Find kth smallest
13+
*/
14+
public class KthSmallestInBST {
15+
public int kthsmallest(TreeNode root, int k) {
16+
List<Integer> inorder = new ArrayList<>();
17+
inorderTraversal(root, k, inorder);
18+
return inorder.get(inorder.size() - 1);
19+
}
20+
21+
private void inorderTraversal(TreeNode root, int k, List<Integer> inorder) {
22+
if (root == null) return;
23+
inorderTraversal(root.left, k, inorder);
24+
if (k == inorder.size()) return;
25+
inorder.add(root.val);
26+
inorderTraversal(root.right, k, inorder);
27+
}
28+
29+
@Test
30+
public void testKthSmallest() {
31+
TreeNode root = new TreeNode(10);
32+
root.left = new TreeNode(7);
33+
root.right = new TreeNode(15);
34+
35+
root.right.right = new TreeNode(17);
36+
37+
root.left.left = new TreeNode(5);
38+
root.left.right = new TreeNode(8);
39+
40+
assertEquals(8, kthsmallest(root, 3));
41+
42+
root.left.left.left = new TreeNode(3);
43+
44+
assertEquals(7, kthsmallest(root,3));
45+
}
46+
}

src/main/java/com/eprogrammerz/examples/algorithm/trees/TreeNode.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.eprogrammerz.examples.algorithm.trees;
22

3-
import lombok.ToString;
4-
53
public class TreeNode {
64
int val;
75
TreeNode left;

src/main/java/com/eprogrammerz/examples/algorithm/trees/TreeTraversal.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class TreeTraversal {
2121
* 4 5 6
2222
*
2323
*
24-
* postorder: 1 2 4 5 3 6
24+
* inorder: 4 2 5 1 3 6
2525
*/
2626

2727
@Before
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.eprogrammerz.examples.algorithm.trees;
2+
3+
import org.junit.Test;
4+
5+
import java.util.LinkedList;
6+
import java.util.Queue;
7+
8+
/**
9+
* Given a binary search tree T, where each node contains a positive integer, and an integer K, you have to find whether or not there exist two different nodes A and B such that A.value + B.value = K.
10+
*
11+
* Return 1 to denote that two such nodes exist. Return 0, otherwise.
12+
*
13+
* Notes
14+
*
15+
* Your solution should run in linear time and not take memory more than O(height of T).
16+
* Assume all values in BST are distinct.
17+
*/
18+
public class TwoSumBinaryTree {
19+
public int t2Sum(TreeNode root, int target) {
20+
Queue<TreeNode> queue = new LinkedList<>();
21+
22+
queue.add(root);
23+
24+
while (!queue.isEmpty()) {
25+
TreeNode node = queue.poll();
26+
int y = target - node.val;
27+
28+
if (exists(root, y, node)) {
29+
return 1;
30+
}
31+
32+
if (node.left != null) {
33+
queue.add(node.left);
34+
}
35+
36+
if (node.right != null) {
37+
queue.add(node.right);
38+
}
39+
}
40+
41+
return 0;
42+
}
43+
44+
private boolean exists(TreeNode node, int target, TreeNode current) {
45+
if (node == null) return false;
46+
if (node.val == target && node != current) return true;
47+
48+
if (node.val > target) return exists(node.left, target, current);
49+
else return exists(node.right, target, current);
50+
}
51+
52+
@Test
53+
public void testT2Sum() {
54+
TreeNode root = new TreeNode(588);
55+
root.left = new TreeNode(134);
56+
root.right = new TreeNode(765);
57+
58+
root.left.left = new TreeNode(35);
59+
root.left.right = new TreeNode(198);
60+
61+
root.left.left.right = new TreeNode(55);
62+
63+
int exists = t2Sum(root, 253);
64+
System.out.println(exists);
65+
}
66+
}

0 commit comments

Comments
 (0)