Accepted Java Solution


  • 4
    V

    public class Solution {

    public void connect(TreeLinkNode root) {
    
        if(root != null){          
            connectNode(root);
            connect(root.left);
            connect(root.right);
        }  
    
    }
    
    public void connectNode(TreeLinkNode root){
       
        if(root.left != null && root.right != null){
            root.left.next = root.right;
        }
        
        if(root.next != null && root.right != null && root.next.left != null){
            root.right.next = root.next.left;
        }
    }
    

    }


Log in to reply
 

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