MemSQL | Phone Screen | Populating Next Right Pointers
Anonymous User
459

Binary tree
Link the node pointer "s" to the next node to right side on same level, null if last node on its level.

 
input:
                    1                  
				 /      \
			   2          3           
			 /  \           \
		  4      5           6       
		 /         \           \
	   7            8           9
	   
output:
	                 1    -- null              
				 /      \
			   2    --    3    -- null       
			 /  \           \
		  4  --  5    ---    6    -- null    
		 /          \          \
	   7    ----      8  ---     9  --- null 
Explaination:
root1.s=null
root2.s=root3
root3.s=null 
root4.s=root5 continue..
	   
  class NodeBT {
    
	int val;
	NodeBT left;
    NodeBT right;
    NodeBT s;
  
    public NodeBT(int val) {
	   left=null;
	   right=null;
	   s=null;
	   this.val=val;
	      }
	 }
Comments (2)