题解 | #二叉树的后序遍历#

二叉树的后序遍历

http://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

public class Solution {
      List<Integer> list = new ArrayList<>();

    public void postorder(TreeNode root) {
        if (root == null)
            return;
        postorder(root.left);
        postorder(root.right);
        list.add(root.val);
    }

    public int[] postorderTraversal(TreeNode root) {
        postorder(root);
        int[] post = new int[list.size()];
        for (int i = 0; i < post.length; i++) {
            post[i] = list.get(i);
        }
        return post;
    }
}
全部评论

相关推荐

未来十年java最好的一年:为什么不要JAVA,为什么
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务