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 ...
随机推荐
- IDEA使用介绍
https://blog.csdn.net/qq_27093465/article/details/77449117
- 坑爹的AMH
坑爹的 AMH的配置文件在这里: nginx 配置文件1:/usr/local/nginx-generic-1.6/conf/nginx.conf nginx 配置文件2:/home/wwwroot/ ...
- 字符串全排列 java实现
经常会遇到字符串全排列的问题.例如:输入为{‘a’,’b’,’c’},则其全排列组合为abc,acb,bac,bca,cba,cab.对于输入长度为n的字符串数组,全排列组合为n!种. package ...
- RK3288 USB UVC camera 摄像头 VIDIOC_DQBUF Failed!!! err[I/O error]
RK3288 Android5.1 多个品牌USB摄像头 同一块主板和代码,大部分品牌的USB摄像头可以正常使用,只有某一款USB摄像头不能使用. 插上摄像头,底层可以识别到摄像头. &l ...
- 工作JS总结
获取 <inpout type="checkbox" value="1" /> 多选项的value /*获取checkbox的全部选中项 使用方法: ...
- struts2学习(10)struts2国际化
一.国际化简介: 二.struts2国际化设置: struts.xml: <?xml version="1.0" encoding="UTF-8" ?&g ...
- 20181105_线程之Task
Task是基于.net Framework3.0框架, Task使用的线程也是来自于ThreadPool 多线程的两个意义: 优化体验(常见于不卡界面), 提升运行速度(不同线程可以分担运算任务) 总 ...
- Druid.io系列(四):索引过程分析
原文链接: https://blog.csdn.net/njpjsoftdev/article/details/52956083 Druid底层不保存原始数据,而是借鉴了Apache Lucene.A ...
- golang代码覆盖率
写了自动化,就得统计代码覆盖率= = 原链接:https://www.elastic.co/blog/code-coverage-for-your-golang-system-tests 其实看了原链 ...
- SVN更新或提交时出现冲突该如何解决
解决版本冲突的命令.在冲突解决之后,需要使用svnresolved来告诉subversion冲突解决,这样才能提交更新.冲突发生时,subversion会在WorkCopy中保存所有的目标文件版本(上 ...