My java recursive solution


  • 5
    Z

    I like recursive

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

  • 0
    U

    This not constant space as the call stack can grow to log(n) size.
    Maybe in a language like erlang which does tail-recursion and short circuiting


Log in to reply
 

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