LeetCode刷题笔录Binary Tree Post Order Traversal

千笔与学术猹:本科生学术写作效率提升利器 在学术写作领域,AIGC技术正逐步改变传统内容创作方式。通过自然语言处理(NLP)和机器学习算法,智能写作工具能够辅助完成文献综述、结构优化等核心环节。千笔与学术猹的组合应用,实现了从文献管理到论文成稿的全流程覆盖,特别适合本科生处理课程论文、实验报告等学术任务。这类工具的技术价值在于将复杂的学术规范转化为可操作的智能建议,同时通过数据可视化、查重改写等实用功能提升写作质量。在实际应用中,需要注意保持学术诚信,将生成内容作为参考而非直接使用。对于计算机专业学生而言,理解这些工具背后的NLP原理,也能为未来的 阅读详情

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 trivial, could you do it iteratively?

Post order traversal的顺序是先访问左子树,再访问右子树,最后访问自身。这里可以用一个stack来存放待访问节点,并用一个prev指针来存储上一个访问的node,根据prev和curr的关系来决定如何前进。

每次peek一下stack的顶元素,作为curr。

情况分为:

1.prev是curr的parent

这时我们是在从上往下traverse。根据post order traversal的定义,如果curr.left != null,则将curr.left放到Stack里。如果curr.left = null,则将curr.right放进去。如果curr.left和curr.right都是null,说明curr是一个leaf node,访问之并从stack中pop出来。

2.prev是curr的left child

这时我们是在从左子树往上访问,按顺序应该将curr.right放入stack

3.prev是curr的right child

这时我们是从右子树往上访问,应该访问curr本身了。

注意只有当curr被访问时才会从stack里面将其pop出来。

public class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> res = new ArrayList<Integer>();
        if(root == null)
            return res;
        TreeNode prev = null;
        Stack<TreeNode> s = new Stack<TreeNode>();
        s.push(root);
        while(!s.isEmpty()){
            TreeNode curr = s.peek();
            //traversing down the tree
            if(prev == null || prev.left == curr || prev.right == curr){
                if(curr.left != null){
                    s.push(curr.left);
                }
                else if (curr.right != null){
                    s.push(curr.right);
                }
                else{
                    //curr is a leaf node, output it
                    res.add(curr.val);
                    s.pop();
                }
            }
            //traversing up the tree from left
            else if(curr.left == prev){
                if(curr.right != null){
                    s.push(curr.right);
                }
                else{
                    res.add(curr.val);
                    s.pop();
                }
            }
            //traversing up the tree from right
            else if(curr.right == prev){
                res.add(curr.val);
                s.pop();
            }
            prev = curr;
            
        }
        return res;
    }
    
    
}


非递归方法实现 二叉树的 DFS(Pre Order), Post Order Post Order比较难以理解,开辟的lastVisited记录上次访问的点,如果这个点是当前点的右孩子,则打印当前点  #include #include struct node { int data; node* left; node* right; node(int exData) { data = exData; left = NULL; righ 阅读详情

相关推荐

Codesys电子凸轮从入门到应用:手把手教你用MC_CamIn/MC_CamOut实现主从轴同步

本文详细介绍了如何在Codesys平台上使用MC_CamIn和MC_CamOut功能块实现电子凸轮的主从轴同步控制。从环境配置、轴使能到凸轮表创建与动态切换,提供了完整的程序编写指南和调试技巧,帮助工程师快速掌握工业自动化中的高精度运动控制技术。

weixin_26878995的博客 276

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]. Stack Problem, One example

ActiveCoder的博客 384

紫光PG2L100H平台DDR3 IP核调试实战:从AXI接口到状态机设计的避坑指南

本文详细介绍了在紫光PG2L100H FPGA平台上调试DDR3 IP核的实战经验,重点解析了AXI接口的特殊行为和状态机设计的关键技巧。通过具体代码示例和调试方法,帮助开发者规避常见陷阱,实现高性能DDR3控制器设计,达到1.2GB/s的稳定带宽。

weixin_28717969的博客 218

145. Binary Tree Postorder Traversal(二叉树的后序遍历,C++,递归+迭代)

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] Follow up: Recursive solution is trivial, could y...

ha_ha_ha233的博客 397

ubuntu20.04下dsp-slam的运行配置

DSP-SLAM:具有深度形状先验的面向对象的 SLAM

baidu_38431960的博客 3158

Win10系统下Protel 99 SE元件库导入报错?3步搞定File is not recognized问题

本文详细介绍了在Win10系统下解决Protel 99 SE元件库导入报错'File is not recognized'的3步终极方案。通过手动修改配置文件、使用专用工具和优化系统兼容性设置,帮助用户快速解决这一经典PCB设计软件的兼容性问题,提升工作效率。

m3n5b7v8c9x的博客 792

编程--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]. 后序遍历:根据左右根的顺序遍历的,用递归的方法就比较简单了 /...

snowyuuu的博客 268

MIKE水动力笔记18_如何将dfsu流场模拟结果的数据导出成txt文件

MIKE的模拟结果dfsu文件的数据是可以导出的,导出格式为xyz,我们也可以将其改后缀改为txt文本格式,其中包含了某一时刻下所有网格坐标点的数据。

晏长街的博客 4405

LeetCode—binary-tree-postorder-traversal(二叉树的后序遍历)—Java

题目描述: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].思路解析:后序遍历:helper方法:递归先左子树,递归后右子树,插入到链表中新...

Lynn_Baby的博客 386

【报错解决】OpenClaw 报错 EACCES: permission denied:安装权限不足

本文针对国产操作系统环境下使用 npm 安装 OpenClaw 时出现的 EACCES 权限错误,提供了专业解决方案。文章首先分析了问题本质是 npm 全局安装路径与 Linux 权限模型的冲突,然后给出三种解决方案:1)配置用户级 npm 全局目录(推荐);2)使用 nvm 管理 Node.js;3)临时使用 sudo(不推荐)。特别强调在企业环境中应避免使用 root 权限安装,并详细说明了如何正确设置用户级 npm 路径,确保符合生产环境的安全审计要求。该方案同样适用于 OpenEuler、OpenC

博客 3190

微信小程序 - 详细实现随机生成订单号,微信小程序创建订单号思路,不重复订单号生成的方法,小程序自动生成固定规则的订单号流水号(必须是唯一不重复的订单号,生成唯一订单号)详细生成方法,示例代码教程最新

微信小程序,vue2,vue3,订单号,流水号思路,微信小程序 详细实现随机生成订单号(JavaScript),随机生成商户订单号,根据当前时间随机生成流水号或者订单号,微信小程序之商家订单,订单号随机生成器,自动生成固定规则的订单号(前缀+采购日期+6位流水号)微信小程序 生成外卖顺序号,生成订单号或流水号简单方法,实现订单号生成唯一的解决方案,如何按日期生成订单号,14位编号=8位日期(+6位流水号),自动生成订单号(固定+日期+数字),销售订单号自动生成。在微信小程序开发中,详解实现随机生成创建订单号

🏆 前端领域优质创作者 1185

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

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

weixin_30295091的博客 157

App爬虫实战篇-以华为真机手机爬取集换社的app为例

在开始学习这篇文章之前,建议你先按照之前2篇文章(和),配置必要的环境,才可以继续完成本章节内容。

youbingchen的博客 780

【LeetCode】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] 题解:用非递归方式给出二叉树的后序遍历序列,正常想法直接用个栈但是后序是L-R-root的顺序...

天行健,君子以自强不息 247

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

树的前序与后序遍历 (Pre-order Post-order Traversal)

引子公司里一项目使用work flow, 使用的是JBoss jBPM.存储为xml格式,需求如下1) xml文档和business model: node class 实例之间的相互转换2) 检查整个tree的高度和宽度,避免用户创建太深或者太宽的work flow3) 检查node的名称,不容许有重复的命名xml 样例

FireCoder的专栏 5816

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

6. binary-tree-postorderTraversal 二叉树后序遍历

题目描述 Given a binary tree, return the postorder traversal of its nodes' values. 对给定二叉树后序遍历 题目解析 二叉树的后序遍历,网上很很多文章写得很好,这里不在赘述,可以用递归和遍历两个方法解决。 方法一:递归方法代码如下 /** * Definition for binary tree * stru...

Zpeg的博客 400

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

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 /

iamxiaofeifei的博客 563

树 练习题2

练习题

weixin_60833644的博客 2902

No. 06 - Post-order Traversal Sequences of Binary Search Trees

No. 06 - Post-order Traversal Sequences of Binary Search Trees Problem: Determine whether an input array is a post-order traversal sequence of a binary tree or not. If it is, return true; otherwi

一个菜鸟 788

5.1.3—二叉树的遍历—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 trivial, could you

seu_nuaa_zc的博客 599

[Leetcode] Binary tree postorder traversal二叉树后序遍历

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

weixin_30633949的博客 147

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

原题: Given a binary tree, return the postorder traversal of its nodes' values. =&gt;给定一个二叉树,给出后序遍历的所有节点值 For example: =&gt;例如 Given binary tree {1,#,2,3}, =&gt;给定一个二叉树{1,#,2,3} 1 \ ...

836811384 214

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

u010814766的博客 683

LeetCode 二叉树后序遍历(binary-tree-postorder-traversal)

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

weixin_30535167的博客 128

leetcode-102. Binary Tree Level Order Traversal 二叉树的层次遍历

  Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree [3,9,20,null,null,15,7], 3 ...

别说话写代码的博客 350

LeetCode刷题笔记(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 Outpu...

Vensmallzeng的博客 236
上一篇: LeetCode刷题笔录Word Search
下一篇: LeetCode刷题笔录Construct Binary Tree from Preorder and Inorder Traversal
likecool21
博客等级 码龄14年 14粉丝 123原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值