Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit46b313b

Browse files
committed
03.01 (1) insert into BST
1 parentb2c3844 commit46b313b

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+
packageInsertNodeInABinarySearchTree;
14+
15+
importcom.leetcode.TreeNode;
16+
17+
/** see test {@link InsertNodeInABinarySearchTree.SolutionTest } */
18+
publicclassSolution {
19+
20+
publicTreeNodeinsertNode(TreeNoderoot,TreeNodenode) {
21+
TreeNodex =root;
22+
while (x !=null) {
23+
if (x.val >node.val) {
24+
if (x.left ==null) {
25+
x.left =node;
26+
returnroot;
27+
}else {
28+
x =x.left;
29+
}
30+
}elseif (x.val <node.val) {
31+
if (x.right ==null) {
32+
x.right =node;
33+
returnroot;
34+
}else {
35+
x =x.right;
36+
}
37+
}else {
38+
returnroot;
39+
}
40+
}
41+
returnnode;
42+
}
43+
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
packageInsertNodeInABinarySearchTree;
2+
3+
importstaticorg.junit.Assert.*;
4+
5+
importorg.junit.After;
6+
importorg.junit.Before;
7+
importorg.junit.Rule;
8+
importorg.junit.Test;
9+
importorg.junit.rules.Timeout;
10+
11+
importcom.leetcode.TreeNode;
12+
13+
publicclassSolutionTest {
14+
15+
/** Test method for {@link InsertNodeInABinarySearchTree.Solution } */
16+
Solutionsolution;
17+
18+
@Rule
19+
publicTimeoutglobalTimeout =newTimeout(200);
20+
21+
@Before
22+
publicvoidsetUp()throwsException {
23+
solution =newSolution();
24+
}
25+
26+
@After
27+
publicvoidtearDown()throwsException {
28+
solution =null;
29+
}
30+
31+
// null => 1
32+
@Test
33+
publicvoidTest1() {
34+
TreeNoderoot =null;
35+
TreeNodenode =newTreeNode(1);
36+
TreeNodeactual =solution.insertNode(root,node);
37+
TreeNodeexpected =newTreeNode(1);
38+
assertTrue(TreeNode.isSameTree(actual,expected));
39+
}
40+
41+
// 1 => 1
42+
// \
43+
// 2
44+
@Test
45+
publicvoidTest2() {
46+
TreeNoderoot =newTreeNode(1);
47+
TreeNodenode =newTreeNode(2);
48+
TreeNodeactual =solution.insertNode(root,node);
49+
TreeNodeexpected =newTreeNode(1);
50+
expected.right =newTreeNode(2);
51+
assertTrue(TreeNode.isSameTree(actual,expected));
52+
}
53+
54+
// 3 => 3
55+
// /
56+
// 2
57+
@Test
58+
publicvoidTest3() {
59+
TreeNoderoot =newTreeNode(3);
60+
TreeNodenode =newTreeNode(2);
61+
TreeNodeactual =solution.insertNode(root,node);
62+
TreeNodeexpected =newTreeNode(3);
63+
expected.left =newTreeNode(2);
64+
assertTrue(TreeNode.isSameTree(actual,expected));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp