What's wrong with my code?


  • 0
    L
     public class Solution {
            public void connect(TreeLinkNode root) {
                if (root == null) {
                    return;
                }
                if (root.left == null) {
                    return;
                }
                connect(root.left);
                connect(root.right);
                root.left.next = root.right;
                if (root.left.right != null) {
                    root.left.right.next = root.right.left;
                }
            }
        }
    

    Thank you!


  • 0
    S

    root.left.right.right will not get connected to root.right.left.left. Recursion doesn't work here because the left side and the right side are not independent.


Log in to reply
 

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