145. Binary Tree Postorder Traversal #
题目 #
给定二叉树的根节点 root
,返回它节点值的 后序 遍历。
思路 #
代码 #
暴力解 #
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 void traversal(TreeNode root, List<Integer> ans) {
if (root == null) return;
traverse(root.left, ans);
traverse(root.right, ans);
ans.add(root.val);
}
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> ans = new ArrayList<>();
traverse(root, ans);
return ans;
}
}