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 ...
随机推荐
- Spring系列-SpringBase+IOC
Spring 一.前言 Thinking is more important than learning 本文主要讲述spring 以及它的 IOC原理 代码地址:https://gitee.com/ ...
- 让PHP能够调用C的函数-FFI扩展
在大型公司中,一般会有很我编程语言的配合.比如说让 Java 来做微服务层,用 C++ 来进行底层运算,用 PHP 来做中间层,最后使用 JS 展现效果.这些语言间的配合大部分都是通过 RPC 来完成 ...
- php curl下载文件由于空格导致下载文件失败
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...
- P4199-万径人踪灭【FFT】
正题 题目链接:https://www.luogu.com.cn/problem/P4199 题目大意 给出一个只包含\(a,b\)的字符串 求有多少个不连续的回文子序列(字母回文,位置对称) \(1 ...
- 面试必问:Java 垃圾回收机制
摘要:垃圾回收机制是守护线程的最佳示例,因为它始终在后台运行. 本文分享自华为云社区<一文带你了解Java 中的垃圾回收机制>,作者:海拥. 介绍 在 C/C++ 中,程序员负责对象的创建 ...
- 踩坑系列《一》数据库建表权限 CREATE command denied to user for table
今天在表中用Navicat连接服务器上的mysql账号进行建表,报了个这样类似的错, CREATE command denied to user for table 是数据库权限设置的问题,所以无法进 ...
- 11204备库升级PSU
某系统PSU为11.2.0.4.190115,备库打补丁20年10月份(31537677)由readme可知:1.要求OPatch要求为11.2.0.3.23或之后的版本.2.非rac环境打补丁需要关 ...
- 11.4.2 LVS—NAT
Virtual Server via NAT(VS-NAT) 用地址翻译实现虚拟服务器。地址转换器有能被外界访问到的合法IP地址,它修改来自专有网络的流出包的地址。外界看起来包是来自地址转换器本身,, ...
- bzoj2038 小z的袜子 (莫队)
题目大意 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色的袜子中找出一双来穿.终于有一天,小Z再也无法忍受这恼人的找袜子过程,于是他决定听天由命-- 具体来说,小Z把这N只袜子从1到N编 ...
- 随机生成文章的AI(C++)
#include <iostream> #include <cstdlib> #include <ctime> #include <fstream> u ...