Skip to content

Commit eac6a74

Browse files
committed
Second smallest element in BST: done
1 parent 176b5a8 commit eac6a74

File tree

4 files changed

+127
-8
lines changed

4 files changed

+127
-8
lines changed

src/main/java/com/rampatra/base/BinarySearchTree.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,16 @@
55
import static java.lang.System.out;
66

77
/**
8-
* Created by IntelliJ IDEA.
9-
* User: rampatra
10-
* Date: 4/19/15
11-
* Time: 6:36 PM
12-
* To change this template go to Preferences | IDE Settings | File and Code Templates
8+
* A binary search tree is a binary tree in which every node fits a specific ordering property: all left
9+
* descendents <= n < all right descendents. This must be true for each node n.
10+
* <p>
11+
* Note: The definition of a binary search tree can vary slightly with respect to equality. Under some definitions, the
12+
* tree cannot have duplicate values. In others, the duplicate values will be on the right or can be on either side. All
13+
* are valid definitions, but you should clarify this with your interviewer
14+
*
15+
* @author rampatra
16+
* @since 4/19/15
17+
* @param <E>
1318
*/
1419
public class BinarySearchTree<E extends Comparable<E>> extends BinaryTree<E> {
1520

src/main/java/com/rampatra/trees/CheckForBST.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,15 @@
99
import static java.lang.System.out;
1010

1111
/**
12-
* Created by IntelliJ IDEA.
12+
* A binary search tree is a binary tree in which every node fits a specific ordering property: all left
13+
* descendents <= n < all right descendents. This must be true for each node n.
14+
* <p>
15+
* Note: The definition of a binary search tree can vary slightly with respect to equality. Under some definitions, the
16+
* tree cannot have duplicate values. In others, the duplicate values will be on the right or can be on either side. All
17+
* are valid definitions, but you should clarify this with your interviewer
1318
*
1419
* @author rampatra
1520
* @since 6/26/15
16-
* @time: 7:14 PM
1721
*/
1822
public class CheckForBST {
1923

src/main/java/com/rampatra/trees/LeastCommonAncestor.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void leastCommonAncestor() {
4242
* @return
4343
*/
4444
public static <E extends Comparable<E>> BinaryNode<E> leastCommonAncestor(BinaryNode<E> node, E value1, E value2) {
45-
if (node == null || value1.compareTo(value2) > 0) throw new NoSuchElementException();
45+
if (node == null || value1 == null || value2 == null || value1.compareTo(value2) > 0) {
46+
throw new NoSuchElementException();
47+
}
4648

4749
if (value1.compareTo(node.value) <= 0 && value2.compareTo(node.value) >= 0) {
4850
return node;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.rampatra.trees;
2+
3+
/**
4+
* Given a Binary Search Tree, find out the second smallest element in the tree.
5+
*
6+
* @author rampatra
7+
* @since 2019-04-02
8+
*/
9+
public class SecondSmallestInBST {
10+
11+
private static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
TreeNode(int val) {
17+
this.val = val;
18+
}
19+
}
20+
21+
private static TreeNode getSecondSmallestNode(TreeNode root) {
22+
if (root == null) return null;
23+
24+
TreeNode curr = root;
25+
26+
if (curr.left == null) {
27+
if (curr.right == null) {
28+
return null;
29+
} else {
30+
return curr;
31+
}
32+
}
33+
34+
while (curr.left.left != null) {
35+
curr = curr.left;
36+
}
37+
38+
if (curr.left.right != null) {
39+
return curr.left.right;
40+
} else {
41+
return curr;
42+
}
43+
}
44+
45+
public static void main(String[] args) {
46+
/*
47+
The BST looks like:
48+
49+
4
50+
/ \
51+
2 8
52+
/ \ / \
53+
1 3 6 9
54+
/
55+
0
56+
57+
*/
58+
TreeNode treeRoot = new TreeNode(4);
59+
treeRoot.left = new TreeNode(2);
60+
treeRoot.right = new TreeNode(8);
61+
treeRoot.left.left = new TreeNode(1);
62+
treeRoot.left.right = new TreeNode(3);
63+
treeRoot.left.left.left = new TreeNode(0);
64+
treeRoot.right.left = new TreeNode(6);
65+
treeRoot.right.right = new TreeNode(9);
66+
67+
System.out.println(getSecondSmallestNode(treeRoot).val);
68+
69+
/*
70+
The BST looks like:
71+
72+
4
73+
/ \
74+
2 8
75+
/ \ / \
76+
1 3 6 9
77+
78+
*/
79+
treeRoot = new TreeNode(4);
80+
treeRoot.left = new TreeNode(2);
81+
treeRoot.right = new TreeNode(8);
82+
treeRoot.left.left = new TreeNode(1);
83+
treeRoot.left.right = new TreeNode(3);
84+
treeRoot.right.left = new TreeNode(6);
85+
treeRoot.right.right = new TreeNode(9);
86+
87+
System.out.println(getSecondSmallestNode(treeRoot).val);
88+
89+
/*
90+
The BST looks like:
91+
92+
4
93+
/ \
94+
2 8
95+
\ / \
96+
3 6 9
97+
98+
*/
99+
treeRoot = new TreeNode(4);
100+
treeRoot.left = new TreeNode(2);
101+
treeRoot.right = new TreeNode(8);
102+
treeRoot.left.right = new TreeNode(3);
103+
treeRoot.right.left = new TreeNode(6);
104+
treeRoot.right.right = new TreeNode(9);
105+
106+
System.out.println(getSecondSmallestNode(treeRoot).val);
107+
}
108+
}

0 commit comments

Comments
 (0)