LeetCode 145. 二叉树的后序遍历 Binary Tree Postorder Traversal

高级数据结构及算法:树的遍历 文章目录1. 多叉树遍历1.1 深度优先遍历先序遍历后序遍历1.2 广度优先遍历2. 二叉树遍历2.1 深度优先遍历先序遍历后序遍历中序遍历 1. 多叉树遍历 图形演示 1.1 深度优先遍历 深度优先遍历:从根节点开始先沿着树的一个枝遍历到叶子节点,再遍历其他的枝。深度优先遍历又分为先序遍历和后序遍历 先序遍历 上图树的先序遍历结果:A → B → D → G → H → I → C → E → J → F 递归深度优先遍历:先序遍历 以 力扣589题:叉树的前序遍历 为例 /* // Definitio 阅读详情

题目: LeetCode 145. 二叉树的后序遍历

给定一个二叉树,返回它的 后序 遍历。

示例:

输入: [1,null,2,3]
1

2
/
3

输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?

递归算法

import java.util.ArrayList;
import java.util.List;

/// 145. Binary Tree Postorder Traversal
/// https://leetcode.com/problems/binary-tree-postorder-traversal/description/
/// 二叉树的后序遍历
/// 时间复杂度: O(n), n为树的节点个数
/// 空间复杂度: O(h), h为树的高度
public class Solution145 {

    // Definition for a binary tree node.
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x) { val = x; }
    }

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        postorderTraversal(root, res);
        return res;
    }

    private void postorderTraversal(TreeNode node, List<Integer> list){
        if(node != null){
            postorderTraversal(node.left, list);
            postorderTraversal(node.right, list);
            list.add(node.val);
        }
    }
}

非递归算法

// Definition for a binary tree node.
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x) { val = x; }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// Non-Recursive
// Using a tag to record whether the node has been visited
//
// Time Complexity: O(n), n is the node number in the tree
// Space Complexity: O(h), h is the height of the tree
public class Solution1 {

    private class TagNode{
        TreeNode node;
        boolean isFirst;
        TagNode(TreeNode node){
            this.node = node;
            this.isFirst = false;
        }
    };

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<TagNode> stack = new Stack<>();
        TreeNode cur = root;
        while(cur != null || !stack.empty()){

            while(cur != null){
                stack.push(new TagNode(cur));
                cur = cur.left;
            }

            TagNode tagNode = stack.pop();
            cur = tagNode.node;
            if(tagNode.isFirst == false){
                tagNode.isFirst = true;
                stack.push(tagNode);
                cur = cur.right;
            }
            else{
                res.add(cur.val);
                cur = null;
            }
        }
        return res;
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// Non-Recursive
// Using two stacks, Reverse Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public class Solution2 {

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<TreeNode> stack = new Stack<>();
        Stack<Integer> output = new Stack<>();

        stack.push(root);
        while(!stack.empty()){

            TreeNode cur = stack.pop();
            output.push(cur.val);

            if(cur.left != null)
                stack.push(cur.left);
            if(cur.right != null)
                stack.push(cur.right);
        }

        while(!output.empty())
            res.add(output.pop());
        return res;
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.LinkedList;

// Non-Recursive
// Using two stacks, Reverse Preorder Traversal!
//
// Time Complexity: O(n)
// Space Complexity: O(n)
public class Solution3 {

    public List<Integer> postorderTraversal(TreeNode root){

        Stack<TreeNode> stack = new Stack<>();
        LinkedList<TreeNode> output = new LinkedList<>();

        TreeNode p = root;
        while(p != null || !stack.isEmpty()){
            if(p != null){
                stack.push(p);
                output.push(p);
                p = p.right;
            }
            else{
                p = stack.pop();
                p = p.left;
            }
        }

        ArrayList<Integer> res = new ArrayList<>();
        while(!output.isEmpty())
            res.add(output.pop().val);
        return res;
    }
}

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public class Solution4 {

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;

        stack.push(root);
        while(!stack.empty()){

            TreeNode cur = stack.pop();
            if((cur.left == null && cur.right == null) ||
                    (pre != null && pre == cur.left && cur.right == null) ||
                    (pre != null && pre == cur.right)){
                res.add(cur.val);
                pre = cur;
            }
            else{
                stack.push(cur);
                if(cur.right != null)
                    stack.push(cur.right);
                if(cur.left != null)
                    stack.push(cur.left);
            }
        }
        return res;
    }
}

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public class Solution5 {

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        TreeNode cur = root;

        while(cur != null || !stack.empty()){

            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }

            cur = stack.pop();
            if(cur.right == null || pre == cur.right){
                res.add(cur.val);
                pre = cur;
                cur = null;
            }
            else{
                stack.push(cur);
                cur = cur.right;
            }
        }
        return res;
    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

// Classic Non-Recursive
// Using a pre pointer to record the last visted node
//
// Time Complexity: O(n)
// Space Complexity: O(h)
public class Solution6 {

    public List<Integer> postorderTraversal(TreeNode root) {

        ArrayList<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;

        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        TreeNode cur = root;

        while(cur != null || !stack.empty()){

            if(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            else{
                cur = stack.pop();
                if(cur.right == null || pre == cur.right){
                    res.add(cur.val);
                    pre = cur;
                    cur = null;
                }
                else{
                    stack.push(cur);
                    cur = cur.right;
                }
            }
        }
        return res;
    }
}
实战Nocobase插件开发:如何用yarn pm命令快速创建和测试API接口插件 本文详细介绍了如何使用Nocobase插件开发工具快速创建和测试API接口插件,涵盖从环境配置、插件骨架生成到数据模型定义和接口实现的完整流程。通过yarn pm命令简化开发步骤,帮助开发者高效构建支持CRUD的API插件,适用于低代码平台开发场景。 阅读详情

相关推荐

基于决策树的英雄联盟比赛胜负预测(含完整代码)

在代码中设置随机种子(例如 RANDOM_SEED = 2024)的主要目的是为了确保每次运行代码时,随机过程的结果是一致的。通过绘制不同参数组合下的准确率图,直观地展示了参数对模型性能的影响,帮助读者更好地理解和选择合适的参数。通过设置相同的随机种子(伪随机数),可以确保每次运行代码时,随机数生成器生成的随机数序列是相同的。如果在开发过程中遇到问题,设置固定的随机种子可以帮助你更容易地定位问题,因为每次运行代码时的数据和结果都是一样的。在科学研究和实验中,结果的可重复性是非常重要的。

oisflo的博客 1919

【leetcode/多叉树】多叉树的后序遍历(递归)

问题描述: 给定一个 N 叉树,返回其节点值的后序遍历。 例如,给定一个3叉树: 返回其后序遍历: [5,6,3,2,4,1]. 说明:递归法很简单,你可以使用迭代法完成此题吗? 基本思路: 递归特别简单: AC代码: /* // Definition for a Node. class Node { public: int val; ...

qq_43338695的博客 1293

Redhawk解析PAD / IOPAD的实现方法

Redhawk解析IOPAD的实现方法1. IOPAD DEF的概念说明2. 生成 IOPAD GDS文件3. 生成 IOPAD DEF文件4. Redhawk解析过程中读取IOPAD DEF 本文主要简述【Redhawk解析IOPAD】的实现原理方法,具体脚本没有放在这里。 1. IOPAD DEF的概念说明 2. 生成 IOPAD GDS文件 注: getinfo2IODEF.tcl 是基于innovus获取IOPAD的摆放信息以及库信息。 run_cerate_iopadgds.csh 是基于c

kobayashiyou的博客 3155

N叉树的后序遍历(3种实现)

N叉树的后序遍历(力扣:590) 给定一个 N 叉树,返回其节点值的后序遍历。

卜大爷的博客 2万+

STC8单片机低功耗延时优化实践:从循环等待到定时唤醒

本文深入探讨了STC8单片机低功耗延时优化的核心方法,即利用掉电唤醒定时器(WKT)替代传统的循环等待,实现从“空转耗电”到“定时唤醒”的转变。通过实战代码演示了如何编写低功耗延时函数,将待机电流从mA级降至μA级,并分析了精度、唤醒时间等关键权衡点,为电池供电的物联网设备提供了有效的续航优化方案。

pea55的博客 325

《Python Web项目集锦》-专栏介绍和目录

本专栏专注于Python Web项目,涉及内容包含FastAPI、Django、Flask等,所有项目代码完整且可直接运行。不管是零基础还是有经验的开发,相信都能从本专栏获益。有任何问题欢迎私聊博主,专栏持续更新中...

数据知道的博客 5214

多叉树的 前序、后序、层序遍历,非递归

1、描述 590给定一个 N 叉树,返回其节点值的后序遍历。 2、关键字 N叉树,后序遍历 3、思路 前序遍历,stack, 思考前序遍历是:根左右,后序遍历是:左右根 如果把后序遍历根节点和孩子节点倒过来,就是和前序遍历类型一样了。而前序遍历是:左右, 后序遍历是右左,所以在处理孩纸节点时候,用stack暂存当前节点的孩子节点,输出逆置答案时,把stack弹出就好了, 4、notes 1、前序遍历就是队列,层序遍历也是队列,后序遍历就是用栈暂存逆序,最后结果序列再逆置一下 2、层序遍历是取第一个元素,把

Tlwhisper的博客 1169

FFmpeg 开发(01):FFmpeg 编译和集成

尽管 FFmpeg 功能强大,但是由于其采用的是带有传染性的 LGPL/GPL 开源协议,所以一些大厂基本上都是自己独立开发类似的音视频处理库,甚至在接口和组织模块上模仿 FFmpeg 。

字节流动 1万+

三、高级数据结构和算法:树的遍历

树的遍历(递归和迭代)

小脑斧的博客 1748

流量PID控制(开度前馈量计算+辅助PID)

PLC通过伯努利方程近似计算水箱流量(FC)-优快云博客文章浏览阅读1.6k次。本文介绍了如何使用PLC通过伯努利方程近似计算水箱中的液体流量,主要涉及流量计算、模型验证、梯形图编程及STL代码实现。适合自动化领域的学习者参考。单容双容水箱建模(simulink仿真+PLC代码)_水箱系统的建模方法-优快云博客文章浏览阅读2k次。该博客介绍了如何使用伯努利方程建立单容和双容水箱模型,详细阐述了从理论建模到PLC编程的实现过程。

RXXW_Dor的博客 848

SIMULINK开发项目实例 1000 例专栏之第960例--基于电力系统稳定性的建模与仿真场景:-含分布式电源的配电网电压稳定性研究

间歇性与随机性:例如,太阳能依赖于日照强度,风能依赖于风速,这使得它们的输出功率具有不确定性。逆变器接口:大多数分布式电源通过逆变器接入电网,这对电网的动态响应特性产生了影响。位置分散:通常分布在用户侧或靠近负荷中心的位置。本文详细介绍了如何使用Simulink结合Simscape Electrical 构建一个用于研究含分布式电源配电网电压稳定性的模型,并提供了详细的仿真流程说明。

xiaoheshang_123的博客 154

LeetCode 力扣 145. 二叉树的后序遍历 binary tree postorder traversal

大家觉得写还可以,可以点赞、收藏、关注一下吧! 也可以到我的个人博客参观一下,估计近几年都会一直更新!和我做个朋友吧!https://motongxue.cn 文章目录145. 二叉树的后序遍历题目描述示例:进阶分析递归版迭代版提交结果 145. 二叉树的后序遍历 题目描述 给定一个二叉树,返回它的 后序 遍历。 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶 递归算法很简单,你可以通过迭代算法完成吗? 分析 递归

motongxue的博客 192

【异常】Claude Code 在 Windows CMD 中出现重影现象的排查与解决

Windows CMD运行Claude Code时出现文字重影、乱码等问题,原因是传统CMD对现代ANSI终端控制序列支持不足。解决方案包括:1)推荐安装Windows Terminal(完全支持ANSI标准);2)使用PowerShell 7;3)优化CMD设置(启用虚拟终端、UTF-8编码);4)关闭Claude Code视觉特效。建议优先使用Windows Terminal以获得最佳体验,这是最彻底的解决方案。其他方案仅能部分改善或缓解问题。

奶爸的编程之路,也就一周冷个三天 1963

Leetcode 145. Binary Tree Postorder Traversal二叉树后序遍历

Given a binary tree, return thepostordertraversal of its nodes' values. Example: Input:[1,null,2,3] 1 \ 2 / 3 Output:[3,2,1] 题目链接:https://leetcode.com/problems/binary-tree-...

salmonwilliam的博客 478

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历

题目:给定一个二叉树,返回它的 后序 遍历。示例:输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1]进阶: 递归算法很简单,你可以通过迭代算法完成吗?代码实现:后序遍历的通用实现可以查看博客:二叉树遍历系列--后序遍历递归版本:class Solution { private List&lt;Integer&gt; p...

zhangzhetaojj的博客 322

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

Leetcode 145.二叉树的后序遍历(Binary Tree Postorder Traversal)

Leetcode 145.二叉树的后序遍历 1 题目描述(Leetcode题目链接)   给定一个二叉树,返回它的 后序 遍历。 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2 题解   二叉树的基本遍历方法之一。 递归方法: # Definition for a ...

qq_39378221的博客 217

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode145 Binary Tree Postorder Traversal 二叉树后序遍历

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] 题源:here;完整实现:here 后序遍历就是:左-&gt;右-&gt;root的遍历方式...

李歇特冯-兹拜因巴哈的博客 133

(Java)leetcode-145 Binary Tree Postorder Traversal (二叉树的后序遍历)

后序遍历的迭代解法。

不思量 3239

Leetcode145. Binary Tree Postorder Traversal二叉树的后序遍历

给定一个二叉树,返回它的后序遍历。 进阶:递归算法很简单,你可以通过迭代算法完成吗? 递归: class Solution { public: vector<int> res; vector<int> postorderTraversal(TreeNode* root) { ...

Ha12312的博客 251

LeetCode 145 Binary Tree Postorder Traversal(二叉树后序遍历)

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive solution is t

nudt_oys的博客 675

Leetcode 145. Binary Tree Postorder Traversal(非递归后序遍历二叉树)

Leetcode 145. Binary Tree Postorder Traversal(非递归后序遍历二叉树) 原题连接 分析 这题的关键在于如何非递归的后序遍历二叉树 关键在于 左右中 = reverse(中右左) 对比分析 先看看先序遍历 顺序是中 左 右 在递归算法里的顺序如下 print(root.val) visit(root.left) visit(root.right) ...

漂漂飘的博客 331

LeetCode 145: 二叉树的后序遍历 Binary Tree Postorder Traversal

题目: 给定一个二叉树,返回它的 后序 遍历。Given a binary tree, return the postorder traversal of its nodes' ...

爱写Bug 211

LeetCode-145二叉树的后序遍历(Binary Tree Postorder Traversal)

LeetCode-145 Given a binary tree, return the postorder traversal of its nodes’ values. Example Input: [1,null,2,3]   1     \      2  &n...

V2beach的博客 201

LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++

LeetCode 145. Binary Tree Postorder Traversal 二叉树的后序遍历 C++ Given a binary tree, return thepostordertraversal of its nodes' values. ...

weixin_30929195的博客 98

LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)

145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历。 LeetCode145. Binary Tree Postorder Traversal 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [3,2,1] 进阶: 递归算法很简单,你可以通...

weixin_30610755的博客 114

【LeetCode】 145. Binary Tree Postorder Traversal 二叉树的后序遍历(Hard)(JAVA)

【LeetCode】 145. Binary Tree Postorder Traversal 二叉树的后序遍历(Hard)(JAVA) 题目地址: https://leetcode.com/problems/binary-tree-postorder-traversal/ 题目描述: Given a binary tree, return the postorder traversal of i...

qq_16927853的博客 365
上一篇: LeetCode 127. 单词接龙 Word Ladder
下一篇: LeetCode 94. 二叉树的中序遍历 Binary Tree Inorder Traversal
KAIKAI_ING
博客等级 码龄12年 6粉丝 70原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值