LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++
Given a binary tree, return the preorder traversal of its nodes' values.
Example:
Input: [,null,,]
\
/
Output: [,,]
Follow up: Recursive solution is trivial, could you do it iteratively?
方法一:使用迭代(C++)
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
stack<TreeNode*> s;
TreeNode* cur=root;
while(!s.empty()||cur){
while(cur){
res.push_back(cur->val);
s.push(cur);
cur=cur->left;
}
cur=s.top();
s.pop();
cur=cur->right;
}
return res;
}
方法二:使用递归,更简单,但是效率较低
void preOrder(TreeNode* root,vector<int>& m){
if(!root)
return;
m.push_back(root->val);
preOrder(root->left,m);
preOrder(root->right,m);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res={};
if(!root)
return res;
preOrder(root,res);
return res;
}
LeetCode 144. Binary Tree Preorder Traversal 二叉树的前序遍历 C++的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 【LeetCode】Binary Tree Preorder Traversal(二叉树的前序遍历)
这道题是LeetCode里的第144道题. 题目要求: 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 进阶: 递归算法很 ...
- 144 Binary Tree Preorder Traversal 二叉树的前序遍历
给定一棵二叉树,返回其节点值的前序遍历.例如:给定二叉树[1,null,2,3], 1 \ 2 / 3返回 [1,2,3].注意: 递归方法很简单,你可以使用迭代方法来解决 ...
- LeetCode:144_Binary Tree Preorder Traversal | 二叉树的前序遍历 | Medium
题目:Binary Tree Preorder Traversal 二叉树的前序遍历,同样使用栈来解,代码如下: struct TreeNode { int val; TreeNode* left; ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- lintcode :Binary Tree Preorder Traversal 二叉树的前序遍历
题目: 二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. 挑战 你能使用非递归实现么? 解题: 通过递 ...
- Leetcode 144 Binary Tree Preorder Traversal 二叉树
二叉树的基础操作:二叉树的先序遍历(详细请看数据结构和算法,任意本书都有介绍),即根,左子树,右子树,实现方法中还有用栈实现的,这里不介绍了 /** * Definition for binary t ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
随机推荐
- ML_Clustering
西瓜书学习...ing K均值 k-means 给定样本集$ D = {X_1,X_2,...X_n} \(,k-means针对聚类所得簇划分\)C = {C_1,C_2,...,C_k}$最小化平方 ...
- CF999E Reachability from the Capital来自首都的可达性
题目大意: 有n个节点m条边,边都是单向的,请你添加最少的边使得起点s到其他与其他每一个点之间都能互相到达 这题一看就是一个缩点啊 其实对于原有的m条边相连的一些点,如果之前他们已经形成了强连通分量( ...
- 真tm郁闷
昨天这时还是信心满满,今天这时就已经彻底颓了. 感觉这次是最接近的了,4个题目都做出来了,怎么还会fail,那边也不说为什么,到底是哪里不足,尽说些没用的. 前后都当了4次炮灰了,以后也不会再冲动了, ...
- WinForm中使用BackgroundWorker异步加载数据并使用进度条
在WinForm程序中,有时会因为加载大量数据导致UI界面假死,这种情况对于用户来说是非常不友好的.因此,在加载大量数据的情况下,首先应该将数据加载放在另一线程中进行,这样保证了UI界面的响应:其次可 ...
- bond模式
1.mode=0(balance-rr)(平衡抡循环策略) 链路负载均衡,增加带宽,支持容错,一条链路故障会自动切换正常链路.交换机需要配置聚合口,思科叫port channel.特点:传输数据包顺序 ...
- postgresql 游标,函数,存储过程使用例子
CREATE OR REPLACE FUNCTION cursor_demo() RETURNS refcursor AS --返回一个游标 $BODY$ declare --定义变量及游标 unbo ...
- ubuntu中连接mssql数据库sqlserver
参考文章 https://blog.csdn.net/fangaoxin/article/details/5386149 (感谢作者) sudo apt-get install tdsodbc sud ...
- 子线程更新UI界面的2种方法
一.一般我们都会在子线程完成一些耗时的操作. 1.Android中消息机制: 2.知识点: Message:消息,其中包含了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队, ...
- symfony generate bundle autoload failed的解决办法
I also encountered this problem,I add new bundle namespace in composer.json"autoload": { & ...
- 【坑】linux目录软连接的相关操作--很容易误操作
写一下文档,记录自己工作中的重大事故,警醒自己以后别犯错. 1)目录不能进行硬连接,只能进行软连接,也就是 ln命令必须加上 -s 参数,如下: [root@ALIYUN:~]#ln /srv/bak ...