Faster and Easier to understand

  • 0

    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);
        private void connectMid(TreeLinkNode left, TreeLinkNode right) {
            if (left == null) return;
   = 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.