Leetcode 144
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
dfs(root,res);
return res;
}
void dfs(TreeNode* root,vector<int>& res){
if(root == NULL) return;
res.push_back(root->val);
dfs(root->left,res);
dfs(root->right,res);
}
};
迭代遍历:
一般我们提到树的遍历,最常见的有先序遍历,中序遍历,后序遍历和层序遍历,它们用递归实现起来都非常的简单。而题目的要求是不能使用递归求解,于是只能考虑到用非递归的方法,这就要用到stack来辅助运算。由于先序遍历的顺序是"根-左-右", 算法为:
1. 把根节点push到栈中
2. 循环检测栈是否为空,若不空,则取出栈顶元素,保存其值,然后看其右子节点是否存在,若存在则push到栈中。再看其左子节点,若存在,则push到栈中。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
if(root == NULL) return res;
stack<TreeNode*> st;
st.push(root);
while(!st.empty()){
TreeNode* p = st.top();
st.pop();
res.push_back(p->val);
if(p->right) st.push(p->right);
if(p->left) st.push(p->left);
}
return res;
} };
——
Leetcode 144的更多相关文章
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java实现 LeetCode 144 二叉树的前序遍历
144. 二叉树的前序遍历 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] /** * Definition for a ...
- Leetcode 144. Binary Tree Preorder Traversal
参考例子:[8,3,1,6,4,7,10,14,13] 8,3,1 和 6,4 说明从root开始,沿着左臂向下寻找leaf 的过程中应该逐个将node.val push入ans. class Sol ...
- Binary Tree Preorder Traversal -- LEETCODE 144
方法一:(迭代) class Solution { public: vector<int> preorderTraversal(TreeNode* root) { vector<in ...
- Java for LeetCode 144 Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- leetcode 144. Binary Tree Preorder Traversal ----- java
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Java [Leetcode 144]Binary Tree Preorder Traversal
题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given bin ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- 【C#】异步的用法
1. C#5.0 加入了async, await关键字. async是在声明异步方法时使用的修饰符, 声明放在返回值之前即可,await表达式则负责消费异步操作, 不能出现在catch或finally ...
- Shell: nohup守护进程化
如果想在终端会话中启动shell脚本,然后让脚本一直以后台模式运行,直到其完成,即使你退出了终端会话,可以使用nohup命令实现.感觉nohup就是将一个进程初始化为一个守护进程. nohup命令运行 ...
- C++中set的用法
set的特性是,所有元素都会根据元素的键值自动排序,set的元素不像map那样可以同时拥有实值(value)和键值(key),set元素的键值就是实值,实值就是键值.set不允许两个元素有相同的键值. ...
- 正则解析json数据
http://tool.chinaz.com/regex http://tool.oschina.net/regex/
- ECMAScript6语法重点(二)
十一.Proxy和Reflect(Proxy保护对象不被外界访问:Object方法移植到Reflect) ①Proxy:原始对象(供应商)通过Proxy(代理商)生成新对象(映射原对象),用户访问的是 ...
- 跳跳虎回家(国庆10.1模拟赛T2)
题目: [题目描述] 跳跳虎在外面出去玩忘了时间,现在他需要在最短的时间内赶回家. 跳跳虎所在的世界可以抽象成一个含有 n 个点的图(点编号从 1 到 n ),跳跳虎现在在 1 号点,跳跳虎的家在 n ...
- socket之基础
链接https://www.cnblogs.com/clschao/articles/9593164.html
- 正则获取 某段 DIV 中 的内容
string html = "<div class='aa'><div class='left'>324324<div>dsfsdf</div> ...
- oracle 存储过程给另一个用户的权限问题
grant execute on (包名)存储过程名称 to 用户名; grant debug on (包名)存储过程名称 to 用户名 grant select on 存储过程名称 to 用户名 ...
- sass、less是什么,如何使用?
一个很好的介绍的SASS,LESS的区别的文档,值get 1.背景介绍 CSS 是一门非程序式语言,没有变量.函数.SCOPE(作用域),需要书写大量看似没有逻辑的代码,不方便维护及扩 展,不利于复用 ...