Faster and Easier to understand


  • 0
    C

    First connect the middle two path, then do the same thing with root.left and root.right.

    public class Solution {
        public void connect(TreeLinkNode root) {
            if (root == null) return;
            connectMid(root.left, root.right);
            connect(root.left);
            connect(root.right);
        }
        private void connectMid(TreeLinkNode left, TreeLinkNode right) {
            if (left == null) return;
            left.next = right;
            connectMid(left.right, right.left);
        }
    }
    

Log in to reply
 

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