Skip to content

Commit 46b313b

Browse files
committed
03.01 (1) insert into BST
1 parent b2c3844 commit 46b313b

File tree

3 files changed

+635
-0
lines changed

3 files changed

+635
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Time : O() ; Space: O()
3+
* @tag : LintCode Copyright; Binary Search Tree
4+
* @by : Steven Cooks
5+
* @date: Aug 26, 2015
6+
***************************************************************************
7+
* Description:
8+
*
9+
*
10+
***************************************************************************
11+
* {@link http://www.lintcode.com/en/problem/insert-node-in-a-binary-search-tree/# }
12+
*/
13+
package InsertNodeInABinarySearchTree;
14+
15+
import com.leetcode.TreeNode;
16+
17+
/** see test {@link InsertNodeInABinarySearchTree.SolutionTest } */
18+
public class Solution {
19+
20+
public TreeNode insertNode(TreeNode root, TreeNode node) {
21+
TreeNode x = root;
22+
while (x != null) {
23+
if (x.val > node.val) {
24+
if (x.left == null) {
25+
x.left = node;
26+
return root;
27+
} else {
28+
x = x.left;
29+
}
30+
} else if (x.val < node.val) {
31+
if (x.right == null) {
32+
x.right = node;
33+
return root;
34+
} else {
35+
x = x.right;
36+
}
37+
} else {
38+
return root;
39+
}
40+
}
41+
return node;
42+
}
43+
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package InsertNodeInABinarySearchTree;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.After;
6+
import org.junit.Before;
7+
import org.junit.Rule;
8+
import org.junit.Test;
9+
import org.junit.rules.Timeout;
10+
11+
import com.leetcode.TreeNode;
12+
13+
public class SolutionTest {
14+
15+
/** Test method for {@link InsertNodeInABinarySearchTree.Solution } */
16+
Solution solution;
17+
18+
@Rule
19+
public Timeout globalTimeout = new Timeout(200);
20+
21+
@Before
22+
public void setUp() throws Exception {
23+
solution = new Solution();
24+
}
25+
26+
@After
27+
public void tearDown() throws Exception {
28+
solution = null;
29+
}
30+
31+
// null => 1
32+
@Test
33+
public void Test1() {
34+
TreeNode root = null;
35+
TreeNode node = new TreeNode(1);
36+
TreeNode actual = solution.insertNode(root, node);
37+
TreeNode expected = new TreeNode(1);
38+
assertTrue(TreeNode.isSameTree(actual, expected));
39+
}
40+
41+
// 1 => 1
42+
// \
43+
// 2
44+
@Test
45+
public void Test2() {
46+
TreeNode root = new TreeNode(1);
47+
TreeNode node = new TreeNode(2);
48+
TreeNode actual = solution.insertNode(root, node);
49+
TreeNode expected = new TreeNode(1);
50+
expected.right = new TreeNode(2);
51+
assertTrue(TreeNode.isSameTree(actual, expected));
52+
}
53+
54+
// 3 => 3
55+
// /
56+
// 2
57+
@Test
58+
public void Test3() {
59+
TreeNode root = new TreeNode(3);
60+
TreeNode node = new TreeNode(2);
61+
TreeNode actual = solution.insertNode(root, node);
62+
TreeNode expected = new TreeNode(3);
63+
expected.left = new TreeNode(2);
64+
assertTrue(TreeNode.isSameTree(actual, expected));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy