[leetcode]Binary Tree Postorder Traversal-二叉树后续遍历 java

注意以下几项

1. 输入若为NULL,返回空列表,而不是NULL,题目没有说清楚

 

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root==null){
            return new ArrayList<Integer>();
        }
        List<Integer> result = new ArrayList<Integer>();
        if(root.left!=null){
            result.addAll(postorderTraversal(root.left));
        }
        if(root.right != null){
            result.addAll(postorderTraversal(root.right));
        }
        result.add(root.val);
        return result;    
    }
}

 

你可能感兴趣的:(LeetCode,算法)