Java solution - level order traversal using constant space


  • 0
    C
            if(root == null){
                return;
            }
            
            TreeLinkNode head = root, n1 = head, n2 = head;
            
            while(n1 != null){
                head = new TreeLinkNode(0);
                n2 = head;
                while(n1 != null){
                    if(n1.left != null){
                        n2.next = n1.left;
                        n2 = n2.next;
                    }
                    
                    if(n1.right != null){
                        n2.next = n1.right;
                        n2 = n2.next;
                    }
                    
                    n1 = n1.next;
                }
                
                n1 = head.next;
            }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.