Very concise Java Solution using O(1) space


  • 0
    public class Solution {
        public void connect(TreeLinkNode root) {
            if(root == null)
                return;
            connect(root.left);
            connect(root.right);
            interConnect(root.left, root.right);
        }
        
        private void interConnect(TreeLinkNode t1, TreeLinkNode t2) {
            if(t1==null && t2==null)
                return;
            t1.next = t2;
            interConnect(t1.right, t2.left);
        }
    }

Log in to reply
 

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