leetcode145题二叉树的后序遍历
题目:
给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历
/**
* 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> res=new ArrayList<Integer>();
PostTraverse(root,res);
return res;
}
public void PostTraverse(TreeNode node,List<Integer> list)
{
if(node==null)
{
return;
}
PostTraverse(node.left,list);
PostTraverse(node.right,list);
list.add(node.val);
}
}
- 学习资料参考代码随想录