题目链接:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/
题目如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> array=new ArrayList<Integer>();
postorder(root,array);//后序遍历:左——》右——》根
return array;
}
public void postorder(TreeNode root,List<Integer> array){
if(root==null) return ;
postorder(root.left,array);
postorder(root.right,array);
array.add(root.val);
}
}