JAVA recursive solution


  • 0
    J
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<Integer>();
        if(root==null){return list;}
        else{
            postorder(root.left,list);
            postorder(root.right,list);
            list.add(root.val);
        }
        return list;
    }
    public void postorder(TreeNode node, List<Integer> list){
        if(node==null)return;
        else{
            postorder(node.left,list);
            postorder(node.right,list);
            list.add(node.val);
        }
    }

  • 0
    D

    Do not need to call postorder() for left and right in the main method.

    posterorder(root, list);
    return list;
    

    is OK.


Log in to reply
 

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