MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / insert

Method insert

Java/complete-binary-tree-inserter.java:34–59  ·  view source on GitHub ↗
(int v)

Source from the content-addressed store, hash-verified

32
33
34 public int insert(int v) {
35 TreeNode n = new TreeNode(v);
36 n.left = n.right = null;
37 TreeNode parent = null;
38 // Add new node to the queue...
39 q.add(n);
40 while(true){
41 parent = q.peek();
42 // If the node's left is null then, insert there.
43 if(parent.left==null){
44 parent.left = n;
45 break;
46 }
47 // If the node's right is null then, insert there....
48 // Now this node can never become parent of new node, so remove it from the queue
49 if(parent.right==null){
50 parent.right = n;
51 q.poll();
52 break;
53 }
54 // else it means the node has left and right children which are not null... so remove it from queue.
55 q.poll();
56 }
57 // Return the parent's value to the caller function...
58 return parent.val;
59 }
60
61 public TreeNode get_root() {
62 return root;

Callers 5

dfsMethod · 0.80
minCameraCoverMethod · 0.80
findPairsMethod · 0.80
removeStonesMethod · 0.80
mergeKListsMethod · 0.80

Calls 1

addMethod · 0.45

Tested by

no test coverage detected