(int v)
| 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; |
no test coverage detected