-
Notifications
You must be signed in to change notification settings - Fork 23
/
Insert Node in a Binary Search Tree .java
executable file
·104 lines (88 loc) · 2.6 KB
/
Insert Node in a Binary Search Tree .java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
E
tags: BST
往Binary Search Tree里面加东西,一定会找到一个合适的leaf加上去。
那么:就是说someNode.left or someNode.right是null时,就是insert node的地方。
找到那个someNode就按照正常的Binary Search Tree规律。
```
/*
Given a binary search tree and a new tree node, insert the node into the tree.
You should keep the tree still be a valid binary search tree.
Example
Given binary search tree as follow, after Insert node 6, the tree should be:
2 2
/ \ / \
1 4 --> 1 4
/ / \
3 3 6
Challenge
Can you do it without recursion?
Tags Expand
LintCode Copyright Binary Search Tree
*/
//2.23.2016 recap
//Find node that has left or right==null. insert node on left/right
public class Solution {
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (node == null || root == null) {
return node;
}
TreeNode dummy = root;
while (root != null) {
if (node.val < root.val) {
if (root.left == null) {
root.left = node;
break;
}
root = root.left;
} else if (node.val > root.val) {
if (root.right == null) {
root.right = node;
break;
}
root = root.right;
}
}
return dummy;
}
}
/*
Previous solution
Thinking process:
Binary Search Tree:
parent must < left node
parent must > right node
use a dummy node runNode to flow around on the binary search tree, compare with target node.
Find the leaf node and add into appropriate pos.
*/
public class Solution {
/**
* @param root: The root of the binary search tree.
* @param node: insert this node into the binary search tree
* @return: The root of the new binary search tree.
*/
public TreeNode insertNode(TreeNode root, TreeNode node) {
if (root == null) {
root = node;
return root;
}
TreeNode runNode = root;
TreeNode parentNode = null;
while (runNode != null) {
parentNode = runNode;
if (runNode.val > node.val) {
runNode = runNode.left;
} else {
runNode = runNode.right;
}
}//while
if (parentNode != null) {
if (parentNode.val > node.val) {
parentNode.left = node;
} else {
parentNode.right = node;
}
}
return root;
}
}
```