An iterative java solution


  • 12
    G
    public void connect(TreeLinkNode root) {
        
        TreeLinkNode n = root;
        
        while(n != null && n.left != null) {
            TreeLinkNode pre = null;
            
            for(TreeLinkNode p = n; p != null; p = p.next) {
                if(pre != null) pre.next = p.left;
                p.left.next = p.right;
                pre = p.right;
            }
            n = n.left;
        }
    }

Log in to reply
 

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