题解 | #二叉树的后序遍历#
二叉树的后序遍历
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;
}
}