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< ...
随机推荐
- 使用函数的递归调用来解决Hanoi(汉诺)塔问题。
#include<stdio.h> void hanoi(int n, char x, char y, char z); void move(char x, char y); int ti ...
- 15个实用的jQuery技术
JQuery是目前最流行的JavaScript框架之一,可以显著的提高用户与网络应用的交互. 今天为大家介绍50有用的jQuery技术: 1.移动Box 2.滑动框和标题 3.数据的可视化:使用HTM ...
- Unity3D研究院之打开Activity与调用JAVA代码传递参数
原地址:http://www.xuanyusong.com/archives/667 Unity for Android 比较特殊,Unity for IOS 打包是将XCODE工程直接交给开发 ...
- Ubuntu 下使用Remmina Remote Desktop client 连接windows server输入法的问题
Ubuntu 自带的Remmina Remote Desktop 用来连接windows,vnc,ssh等非常方便好用, 但我在连接windows 2008 r2 server时遇到一个问题: ...
- Ubuntu环境下nutch2.2.1集成HBase0.94.25
nutch2.2.1集成HBase0.94.25 (详见:http://duguyiren3476.iteye.com/blog/2085973 ) 1. 修改nutch的hbase配置 //将自己的 ...
- CF 86D Powerful array
离线+分块 将n个数分成sqrt(n)块. 对所有询问进行排序,排序标准: 1. Q[i].left /block_size < Q[j].left / block_size (块号 ...
- PowerDesigner修改设计图中文字的字体大小等样式
设计图中默认的字体是对英文比较合适的,中文就看不清楚了,特别不美观.但是可以通过修改“Display Preferences”适应我们的汉字. 我使用的PowerDesigner版本是15.1(测试版 ...
- Matlab中编译C++文件
今天在跑<Robust Object Tracking via Sparsity-based Collaborative Model>这篇文章的代码时候,发现出现如下错误: 发现错误时由于 ...
- window下安装composer and yii2
我的环境是集合包xampp 1,下载composer:下载地址https://getcomposer.org/download/, 点击蓝色字体“Composer-Setup.exe” 2,安装com ...
- kali 安装中文输入法
(1)apt-get install fcitx-googlepinyin (2)你希望继续执行吗?Y (3)连续执行数次 (4)init 6(命令行重启)