【LeetCode】#145二叉树的后序遍历(Binary Tree Postorder Traversal)
题目描述
给定一个二叉树,返回它的 后序 遍历。
示例
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
Description
Given a binary tree, return the postorder traversal of its nodes’ values.
Example
Input: [1,null,2,3]
1
\
2
/
3
Output: [3,2,1]
解法
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<>();
if(root==null)
return res;
helper(root, res);
return res;
}
public void helper(TreeNode node, List<Integer> res){
if(node.left!=null)
helper(node.left, res);
if(node.right!=null)
helper(node.right, res);
res.add(node.val);
return;
}
}