My JAVA solution(accepted)


  • 1
    J
    public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
    
        traverse(root, root.left);
        traverse(root, root.right);
    }
    public void traverse(TreeLinkNode farther, TreeLinkNode cur) {
        if(cur == null)
            return;
        
        if(cur == farther.left) {
            cur.next = farther.right;
        }
        else {
            if(farther.next == null) {
                cur.next = null;
            }
            else {
                cur.next = farther.next.left;
            }
        }
        traverse(cur, cur.left);
        traverse(cur, cur.right);
    }
    

    }


Log in to reply
 

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