binary-tree-postorder-traversal leetcode C++
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?
C++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode *root){
if (!root) return {};
vector<int> res;
stack<TreeNode*> s{{root}};
while(!s.empty()){
TreeNode *t = s.top(); s.pop();
res.insert(res.begin(),t->val);
if (t->left) s.push(t->left);
if (t->right) s.push(t->right);
}
return res;
}
};
binary-tree-postorder-traversal leetcode C++的更多相关文章
- Binary Tree Postorder Traversal leetcode java
题目: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given bin ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- C++版 - LeetCode 145: Binary Tree Postorder Traversal(二叉树的后序遍历,迭代法)
145. Binary Tree Postorder Traversal Total Submissions: 271797 Difficulty: Hard 提交网址: https://leetco ...
- LeetCode 145. 二叉树的后序遍历(Binary Tree Postorder Traversal)
145. 二叉树的后序遍历 145. Binary Tree Postorder Traversal 题目描述 给定一个二叉树,返回它的 后序 遍历. LeetCode145. Binary Tree ...
- LeetCode: Binary Tree Postorder Traversal 解题报告
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 【LeetCode】145. Binary Tree Postorder Traversal (3 solutions)
Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' va ...
- 12. Binary Tree Postorder Traversal && Binary Tree Preorder Traversal
详见:剑指 Offer 题目汇总索引:第6题 Binary Tree Postorder Traversal Given a binary tree, return the po ...
- Binary Tree Preorder Traversal and Binary Tree Postorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
随机推荐
- Jmeter系列(2)- 代理服务器录制脚本
操作步骤 step-1 添加代理服务器 step-2 添加线程组 step-3 添加录制控制器 HTTP代理服务器配置 - HTTP(S) Test Script Recorder TestPlan ...
- Linux系列(40) - 自动同步时间chrony
前言 Centos8开始取消了ntp同步时间,改为chrony同步 chrony工具安装 yum -y install chrony 修改配置文件 将配置文件中的同步服务器修改为国内的时间服务器(推荐 ...
- Shell系列(15)- 数值运算方法
数值运算-方法1 declare -i [root@localhost ~]# aa=11 [root@localhost ~]# aa=22 #给变量aa和bb赋值 [root@localhos ...
- Shell系列(14)- declare声明变量
declare声明变量类型 格式 declare [+/-] [选项] [变量名] 选项 -:给变量设定类型属性 +:取消变量的类型属性 -a :将变量声明为数组型 -i :将变量声明为整数型(int ...
- httprunner版本没有更新问题
使用命令行创建虚拟环境,创建脚手架目录后,使用pycharm打开所创建的脚手架目录. 执行:hrun demo_testcase_request.yml 提示: E:\hrun_ven\test_hr ...
- python+selenium之浏览器滚动条操作
from selenium import webdriver import time #访问百度 driver=webdriver.Ie() driver.get("http://www.b ...
- P4424-[HNOI/AHOI2018]寻宝游戏【结论】
正题 题目链接:https://www.luogu.com.cn/problem/P4424 题目大意 \(n\)个\(m\)位二进制数,开始是一个\(0\). 然后依次对所有二进制数进行\(n\)次 ...
- Linux学习笔记整理-1
内核检测常用的7个命令: fdisk命令:用于检查磁盘使用情况,以及可以对磁盘进行分区. #fdisk -l 列出系统内所有能找到的设备的分区 #fdisk /dev/sda 列出sda磁盘的分区情况 ...
- Vue3 如何修改端口
build哪里去了?config哪里去了?配置都消失了? 对比之前Vue2 发现项目目录改动较大 找了很久才找到配置文件:node_modules\@vue\cli-service\lib\comma ...
- 超简洁,玩转springboot 之springboot自定义start工程
springboot 的start 建一个父工程 不需要其他目录结构,需要注意的是把type的类型改为POM 这样就没有工程的目录结构 因为父工程不需要 给父工程的pom添加依赖 <depend ...