Very simple, easy to understand, Java Solution


  • 1
    J

    This solution relies completely on the tree being "perfect."

    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root==null) {
                return;
            }
            //down-left next = down-right
            if(root.left != null) {
                root.left.next = root.right;
            }
            //down-right next = right->down-left
            if(root.next != null && root.next.left != null) {
                root.right.next = root.next.left;
            }
            connect(root.left);
            connect(root.right);
       }
    }

Log in to reply
 

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