Why is [1,2] failing for my solution not sure ... all other cases seem to be working


  • -1

    public List<Integer> rightSideView(TreeNode root) {
    List<Integer> retList = new ArrayList<>();
    TreeNode leftRoot = root;
    if(root == null){
    return retList;
    }
    if(root.right == null){
    retList.add(root.val);
    return retList;
    }
    while(root != null){
    retList.add(root.val);
    root = root.right;
    }
    while(leftRoot != null){
    leftRoot = leftRoot.left;
    if(leftRoot != null && leftRoot.right !=null){
    retList.add(leftRoot.right.val);
    }
    }
    return retList;
    }


  • 0
    V

    your code is too messy to read,try adjust it


Log in to reply
 

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