Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)
先序遍历,用递归来做,简单的不能再简单了。代码如下:
(以下仅实现了先序遍历,中序遍历类似,后序遍历和这两个思路不一样,具体详见Binary Tree Postorder Traversal)
/**
* 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 {
private:
vector<int> res;
public:
void getOrder(TreeNode* node)
{
if(node==NULL)
return ;
else
{
res.push_back(node->val);
getOrder(node->left);
getOrder(node->right);
}
return ;
}
vector<int> preorderTraversal(TreeNode* root) {
if(root==NULL)
return res;
res.push_back(root->val);
getOrder(root->left);
getOrder(root->right);
return res; }
};
但是题目要求用迭代来做。
迭代的代码如下:
/**
* 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;
stack<TreeNode*> stk;
while(root!=NULL||!stk.empty())
{
if(root!=NULL)
{
res.push_back(root->val);
stk.push(root);
root=root->left;
}
else
{
root=stk.top()->right;
stk.pop();
} }
return res;
}
};
中序遍历迭代代码如下:
/**
* 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> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> stk;
while(root!=NULL||!stk.empty())
{
if(root!=NULL)
{
stk.push(root);
root=root->left;
}
else
{
root=stk.top();
res.push_back(root->val);
stk.pop();
root=root->right;
}
}
return res;
}
};
Binary Tree Preorder Traversal——经典算法的迭代求解(前序,中序,后序都在这里了)的更多相关文章
- 3月3日(3) Binary Tree Preorder Traversal
原题 Binary Tree Preorder Traversal 没什么好说的... 二叉树的前序遍历,当然如果我一样忘记了什么是前序遍历的.. 啊啊.. 总之,前序.中序.后序,是按照根的位置来 ...
- LeetCode 144. 二叉树的前序遍历(Binary Tree Preorder Traversal)
144. 二叉树的前序遍历 144. Binary Tree Preorder Traversal 题目描述 给定一个二叉树,返回它的 前序 遍历. LeetCode144. Binary Tree ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- C++版 - LeetCode 144. Binary Tree Preorder Traversal (二叉树先根序遍历,非递归)
144. Binary Tree Preorder Traversal Difficulty: Medium Given a binary tree, return the preorder trav ...
- 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
二叉树的非递归前序遍历,大抵是很多人信手拈来.不屑一顾的题目罢.然而因为本人记性不好.基础太差的缘故,做这道题的时候居然自己琢磨出了一种解法,虽然谈不上创新,但简单一搜也未发现雷同,权且记录,希望于人 ...
- 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 ...
- 【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 ...
随机推荐
- bzoj1706: [Usaco2007 Nov]relays 奶牛接力跑 (Floyd+新姿势)
题目大意:有t(t<=100)条无向边连接两点,求s到e刚好经过n(n<=10^7)条路径的最小距离. 第一反应分层图,但是一看n就懵逼了,不会写.看了题解之后才知道可以这么玩... 首先 ...
- getElementsByClassName的原生实现
DOM 提供了一个名为 getElementById() 的方法,这个方法将返回一个对象,这个对象就是参数 id 所对应的元素节点.另外,getElementByTagName() 方法会返回一个对象 ...
- HDU5533(水不水?)
Dancing Stars on Me Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Ot ...
- centos上tensorflow一键安装脚本
鉴于tensorflow在centos上安装相当麻烦,特地制作了一个脚本方便以后移植到其它机器上,脚本含有其它python常用包: #! /bin/bash sudo yum install -y ...
- 远端WEB服务器上存在/robots.txt文件
解决方案: 1. 可直接删除(可参考:http://zh.wikipedia.org/wiki/Robots.txt) ,但不利于SEO等 2. 修改Web服务器配置 可以通过Web服务器(如Apac ...
- C语言数组与指针总结
寒假要开始猛刷<剑指offer>,先回顾一下C语言基础做个热身. 指针 相信大家对下面的代码不陌生: ; int *p; p=&i; 这是最简单的指针应用,也是最基本的用法.再来熟 ...
- Cppcheck代码分析上
1.检查点 1.自动变量检查: 返回自动变量(局部变量)指针: 2.越界检查:数组越界返回自动变量(局部变量)指针: 3.类检查:构造函数初始化: 4.内存泄露检查: 5.空指针检查: 6.废弃函数 ...
- Linux中 设置apache,mysql 开机启动
linux开启启动的程序一般放在/etc/rc.d/init.d/里面,/etc/init.d/是其软连接 mysql设为linux服务 cp /usr/local/mysql5/share/mysq ...
- Jmeter-8-FTP测试
1. 此处要深刻理解FTP的用法. 2. Get的时候填写的Remote File 路径/, 此处是相对路径. 实际为/home/user/ 3. Local file 此处要写到具体的文件. 4. ...
- gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...