The question does not say it should be flatten according to a particular order. Why can't I flatten it to some order other than the one in the solution?


  • 0
    4

    The question does not say it should be flatten according to a particular order. Why can't I flatten it to some order other than the one in the solution? The code below flatten a tree but considered wrong.

    '''
    public void flatten(TreeNode root) {
    if(root == null)
    return;
    TreeNode pointer = root;
    while(pointer != null)
    {
    if(pointer.left != null)
    {
    TreeNode rightEnd = pointer;
    while(rightEnd.right != null)
    {
    rightEnd = rightEnd.right;
    }
    rightEnd.right = pointer.left;
    pointer.left = null;
    }
    pointer = pointer.right;
    }
    }
    '''

    Input:
    [1,2,3]
    Output:
    [1,null,3,null,2]
    Expected:
    [1,null,2,null,3]


Log in to reply
 

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