首页 > 其他 > 详细

145. 二叉树的后序遍历

时间:2021-01-03 15:25:55      阅读:25      评论:0      收藏:0      [点我收藏+]

145. 二叉树的后序遍历

地址:https://leetcode-cn.com/problems/binary-tree-postorder-traversal/

//给定一个二叉树,返回它的 后序 遍历。 
//
// 示例: 
//
// 输入: [1,null,2,3]  
//   1
//    //     2
//    /
//   3 
//
//输出: [3,2,1] 
//
// 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 
// Related Topics 栈 树 
// ?? 498 ?? 0

解法1:Java递归解法

二叉树的后序遍历

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<>();
        last(root, list);
        return list;
    }

    public void last(TreeNode node, List<Integer> res) {
        if (node == null) {
            return;
        }
        last(node.left, res);
        last(node.right, res);
        //后序遍历,最后处理根节点
        res.add(node.val);
    }
}

完!

145. 二叉树的后序遍历

原文:https://www.cnblogs.com/nayou/p/14225311.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!