Can anyone help me figure out why my function wrong?


  • 0
    W

    Below are my solution and results. I really cannot understand why my findMostLeft function not do recursive when dealing with TreeLinkNode 9.
    Please help me!! Thank you all a lot!

    public class Solution {
    public void connect(TreeLinkNode root) {
    if(root == null)
    return;
    if(root.left!=null&&root.right!=null){
    root.left.next = root.right;
    root.right.next = findMostLeft(root.next);
    }else if(root.left!=null)
    root.left.next = findMostLeft(root.next);
    else if(root.right!=null)
    root.right.next = findMostLeft(root.next);
    connect(root.left);
    connect(root.right);
    }
    private TreeLinkNode findMostLeft(TreeLinkNode root){
    if(root==null)
    return null;
    if(root.left==null&&root.right==null) {
    return findMostLeft(root.next);
    }
    return root.left==null?root.right:root.left;
    }
    }

    Input:
    {2,1,3,0,7,9,1,2,#,1,0,#,#,8,8,#,#,#,#,7}
    Output:
    {2,#,1,3,#,0,7,9,1,#,2,1,0,#,7,#}
    Expected:
    {2,#,1,3,#,0,7,9,1,#,2,1,0,8,8,#,7,#}


Log in to reply
 

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