-
-
Notifications
You must be signed in to change notification settings - Fork 419
/
complete-binary-tree-inserter.java
64 lines (61 loc) · 1.84 KB
/
complete-binary-tree-inserter.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class CBTInserter {
TreeNode root;
Queue<TreeNode> q = new LinkedList<TreeNode>();
// This queue is a helper to reduce load on insert function..
public CBTInserter(TreeNode root) {
this.root = root;
Queue<TreeNode> temp = new LinkedList(q);
temp.add(root);
q.add(root);
// Prepare a queue for the for our insert function...
while(!temp.isEmpty()){
TreeNode t = temp.poll();
if(t.left!=null){
temp.add(t.left);
q.add(t.left);
}
if(t.right!=null){
temp.add(t.right);
q.add(t.right);
}
}
}
public int insert(int v) {
TreeNode n = new TreeNode(v);
n.left = n.right = null;
TreeNode parent = null;
// Add new node to the queue...
q.add(n);
while(true){
parent = q.peek();
// If the node's left is null then, insert there.
if(parent.left==null){
parent.left = n;
break;
}
// If the node's right is null then, insert there....
// Now this node can never become parent of new node, so remove it from the queue
if(parent.right==null){
parent.right = n;
q.poll();
break;
}
// else it means the node has left and right children which are not null... so remove it from queue.
q.poll();
}
// Return the parent's value to the caller function...
return parent.val;
}
public TreeNode get_root() {
return root;
}
}