跳至主要內容

145_二叉树的后序遍历

T4mako算法深度优先二叉树小于 1 分钟

145_二叉树的后序遍历

简单
class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        ArrayList<Integer> res = new ArrayList<>();
        if(root == null){
            return res;
        }
        inorder(root,res);
        return res;
    }
    public void inorder(TreeNode root,ArrayList<Integer> res){
        if(root.left != null) {
            inorder(root.left, res);
        }
        if(root.right != null){
            inorder(root.right,res);
        }
        res.add(root.val);
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.5