Java recursive

  • 0

    at each level, the sibling is passed down. Right child is the sibling of left child, the left child of the sibling passed down is the sibling of the right child.

        public void connect(TreeLinkNode root) {
            if (root == null || root.left == null && root.right == null) return;
            connect(root, null);
        private void connect(TreeLinkNode root, TreeLinkNode sibling) {
   = sibling;
            if (root.left != null) {
                connect(root.left, root.right);
            if (root.right != null) {
                connect(root.right, sibling == null ? null : sibling.left);

Log in to reply

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