题解 | #二叉树的后序遍历#
二叉树的后序遍历
http://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541
import java.util.*;
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return int整型一维数组
*/
public int[] postorderTraversal (TreeNode root) {
List<Integer> result = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
stack.add(root);
while(!stack.isEmpty()){
TreeNode pop = stack.pop();
if(pop!=null){
result.add(pop.val);
stack.add(pop.left);
stack.add(pop.right);
}
}
int[] r = new int[result.size()];
int k = result.size()-1;
for (int i = result.size() - 1; i >= 0; i--) {
r[k-i] = result.get(i);
}
return r;
}
}