LeetCode Binary Tree Preorder Traversal 先根遍历
题意:给一棵树,求其先根遍历的结果。
思路:
(1)深搜法:
/**
* 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) {
if(!root) return vector<int>();
vector<int> ans(,root->val);
vector<int> tmp=preorderTraversal(root->left);
ans.insert(ans.end(),tmp.begin(),tmp.end());
tmp=preorderTraversal(root->right);
ans.insert(ans.end(),tmp.begin(),tmp.end());
return ans;
} };
AC代码
(2)依然是深搜法:
/**
* 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> ans;
void DFS(TreeNode* t)
{
if(!t) return;
ans.push_back(t->val);
DFS(t->left);
DFS(t->right);
}
vector<int> preorderTraversal(TreeNode* root) {
DFS(root);
return ans;
}
};
AC代码
(3)迭代法:
/**
* 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) {
if(!root) return vector<int>(); stack<pair<TreeNode*,int>> stac;
stac.push(make_pair(root,));//第二个参数用于记录其已经遍历了左/右孩子
vector<int> ans;
while( !stac.empty() )
{
TreeNode* cur=stac.top().first;
if( stac.top().second== ) ans.push_back(cur->val); if(cur->left && stac.top().second==)//没有遍历过孩子的进来
{
cur=cur->left;
stac.top().second=;
stac.push(make_pair(cur,));
}
else if(cur->right && stac.top().second<)//遍历过左孩子或者没有左孩子的才进来
{
cur=cur->right;
stac.top().second=;
stac.push(make_pair(cur,));
}
else stac.pop();//以上两个都进不去,要么遍历完,要么没有孩子
}
return ans;
}
};
AC代码
(4)更叼的迭代法:
/**
* 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) {
if(!root) return vector<int>(); vector<int> ans;
stack<TreeNode *> stac;
stac.push(root); while( !stac.empty() )
{
TreeNode *t=stac.top();
ans.push_back(t->val);
stac.pop();//只需要孩子都压栈,父亲无用
if(t->right) stac.push(t->right);
if(t->left) stac.push(t->left);
}
return ans;
}
};
AC代码
LeetCode Binary Tree Preorder Traversal 先根遍历的更多相关文章
- LeetCode: Binary Tree Preorder Traversal 解题报告
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [leetcode]Binary Tree Preorder Traversal @ Python
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单.应该考察的是使用非递归实现二叉树的先 ...
- LeetCode——Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary t ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 144. Binary Tree Preorder Traversal (二叉树前序遍历)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- leetcode Binary Tree Postorder Traversal 二叉树后续遍历
先给出递归版本的实现方法,有时间再弄个循环版的.代码如下: /** * Definition for binary tree * struct TreeNode { * int val; * Tree ...
- [LeetCode]144. Binary Tree Preorder Traversal二叉树前序遍历
关于二叉树的遍历请看: http://www.cnblogs.com/stAr-1/p/7058262.html /* 考察基本功的一道题,迭代实现二叉树前序遍历 */ public List< ...
随机推荐
- 【POJ】【3071】Football
概率DP kuangbin总结中的第10题 简单的画个比赛图,会发现是一颗完全二叉树,且同一层的子树之间各自独立,只有在合并得到更高一层结果时才结合. 所以我们可以按比赛轮数进行DP,f[i][j]表 ...
- EBP与ESP寄存器的使用
push ebp mov esp,ebp esp是堆栈指针 ebp是基址指针 这两条指令的意思是将栈顶指向ebp的地址 ---------------------------------------- ...
- 【WCF--初入江湖】05 WCF异步编程
05 WCF异步编程 一.服务设计最佳实践 在设计之初,是否用异步,应该由客户端来决定,而不应该去考虑服务的调用者调用的方式. 优点:充分利用多核CPU, 改善用户体验 缺点:滥用异步,会影响性能 二 ...
- Unity3D研究院之Machine动画脚本自动生成AnimatorController
原地址: http://www.xuanyusong.com/archives/2811 以前的项目一直不敢用Machine动画,因为当时立项的时候Machine动画还不成熟,最近项目做得差不多了我能 ...
- C#的cs文件中Response.Clear();Response.ClearHeaders()的作用
在学习一个CS文件,如下:public partial class GetPic : System.Web.UI.Page{ protected void Page_Load(object se ...
- hdu 3758 Factorial Simplification
这题主要是质因数分解!! 求出每个因子的幂,如果有负数,则输出-1: 如果2的幂数为0,这输出0: 最后就是开始凑阶乘了…… #include<iostream> #include< ...
- 读书笔记:7个示例科普CPU Cache
本文转自陈皓老师的个人博客酷壳:http://coolshell.cn/articles/10249.html 7个示例科普CPU Cache (感谢网友 @我的上铺叫路遥 翻译投稿) CPU cac ...
- [hackerrank]Even Odd Query
https://www.hackerrank.com/contests/w5/challenges 简单题,注意整数的0次方是1,奇数. #include <iostream> #incl ...
- java代码实现自动登录功能
通常我们登录某网站,会有选择保存几天,或者是几个星期不用登录,之后输入该网站地址无需登录直接进入主页面,那么这就叫做自动登录,怎么实现呢,下面我以一个小例子来演示一下 登录页面:login.jsp & ...
- MyBatis Mapper 文件例子
转载:http://blog.csdn.net/ppby2002/article/details/20611737 <?xml version="1.0" encoding= ...