144. Binary Tree Preorder Traversal (Tree, Stack)
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,2,3].
Note: Recursive solution is trivial, could you do it iteratively?
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<int> result;
if(!root) return result;
stack<TreeNode* > treeStack;
treeStack.push(root);
TreeNode* current;
while(!treeStack.empty())
{
current = treeStack.top();
treeStack.pop();
result.push_back(current->val);
if(current->right) treeStack.push(current->right);
if(current->left) treeStack.push(current->left);
}
return result;
}
};
144. Binary Tree Preorder Traversal (Tree, Stack)的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 二叉树前序、中序、后序非递归遍历 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 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- 【LeetCode】144. Binary Tree Preorder Traversal (3 solutions)
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 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 on LeetCode in Java
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- 55. Binary Tree Preorder Traversal
Binary Tree Preorder Traversal My Submissions QuestionEditorial Solution Total Accepted: 119655 Tota ...
随机推荐
- vault key 管理工具
Vault is a tool for securely accessing secrets. A secret is anything that you want to tightly contro ...
- 笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题
笔记:服务器压缩方案 来源于 Accept-Encoding: gzip, deflate 问题 事情起因:odoo demo 没有启动web 压缩 目前流行的 web 压缩技术 gzip br 支持 ...
- 聊聊Oracle 11g的Snapshot Standby Database(下)
3.Snapshot Standby行为研究 下面我们分析一下Snapshot Standby的工作性质和行为性质.我们在主库方向研究当前状态. --主库日志情况 SQL> select gro ...
- jdk1.8新特性之lambda表达式
lambda表达式其实就是指一个匿名函数,应用最广泛的就是匿名内部类的简化.在jdk1.8之前,我们定义一个匿名内部类可能需要写一大坨代码,现在有了lambda之后,可以写的很简洁了.但不是说lamb ...
- Linux下的lds链接脚本详解
1. 概论2. 基本概念3. 脚本格式4. 简单例子5. 简单脚本命令6. 对符号的赋值7. SECTIONS命令8. MEMORY命令9. PHDRS命令10. VERSION命令11. 脚本内的表 ...
- idea maven 打包 引用本地jar
1将本地jar包导入到mvn本地库 mvn install:install-file -Dfile=/Users/liuqiang/Documents/gmpl/gmpl-server/lib/ali ...
- EFCodeFirst使用Nuget更新数据库
在MVC开发中,习惯于使用EF作为数据库操作,相对于传统的Ado.Net的数据库操作方式,EF大大的节省了我们手写SQL语句的时间,即便是传统的使用代码生成的方式.EF操作数据库目前分为两种大的方式. ...
- Java程序中不通过hadoop jar的方式访问hdfs
一般情况下,我们使用Java访问hadoop distributed file system(hdfs)使用hadoop的相应api,添加以下的pom.xml依赖(这里以hadoop2.2.0版本 ...
- 使用wifi网卡笔记3---工具wpa_supplicant(STA模式)
1. wpa_supplicant介绍 supplicant是恳求者的意思,是wpa的发起者,是发送认证请求的设备(手机),手机--AP--认证服务器,可用于上述4种"认证/加密" ...
- 使用SpringData出现java.lang.AbstractMethodError
最近学习一下SpringData,在添加SpringData支持的时候,出现了这样的问题: SpringData需要的jar有:spring-data-jpa.jar spring-data-com ...