lintcode 二叉树的前序遍历
2015-12-07 22:05
239 查看
给出一棵二叉树,返回其节点值的前序遍历。
样例
给出一棵二叉树
返回
题解1 递归
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
void preorder(TreeNode *root,vector<int>&result) {
if (root == nullptr)return;
result.push_back(root->val);
preorder(root->left,result);
preorder(root->right,result);
}
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
preorder(root,result);
return result;
}
};
题解 2 栈
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
const TreeNode*p = root;
stack<const TreeNode *>s;
if (p != nullptr) s.push(p);
while (!s.empty()){
p = s.top();
s.pop();
result.push_back(p->val);
if(p->right != nullptr)s.push(p->right);
if(p->left != nullptr)s.push(p->left);
}
return result;
}
};
题解3 Morris
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
TreeNode *p = root, *temp;
while(p != nullptr) {
if(p->left == nullptr) {
result.push_back(p->val);
p = p->right;
} else {
temp = p->left;
while(temp->right != nullptr && temp->right != p) {
temp = temp->right;
}
if(temp->right == nullptr) {
result.push_back(p->val);
temp->right = p;
p = p->left;
} else {
temp->right = nullptr;
p = p->right;
}
}
}
return result;
}
};
样例
给出一棵二叉树
{1,#,2,3},
返回
[1,2,3].
题解1 递归
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
void preorder(TreeNode *root,vector<int>&result) {
if (root == nullptr)return;
result.push_back(root->val);
preorder(root->left,result);
preorder(root->right,result);
}
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
preorder(root,result);
return result;
}
};
题解 2 栈
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
const TreeNode*p = root;
stack<const TreeNode *>s;
if (p != nullptr) s.push(p);
while (!s.empty()){
p = s.top();
s.pop();
result.push_back(p->val);
if(p->right != nullptr)s.push(p->right);
if(p->left != nullptr)s.push(p->left);
}
return result;
}
};
题解3 Morris
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: Preorder in vector which contains node values.
*/
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
vector<int>result;
TreeNode *p = root, *temp;
while(p != nullptr) {
if(p->left == nullptr) {
result.push_back(p->val);
p = p->right;
} else {
temp = p->left;
while(temp->right != nullptr && temp->right != p) {
temp = temp->right;
}
if(temp->right == nullptr) {
result.push_back(p->val);
temp->right = p;
p = p->left;
} else {
temp->right = nullptr;
p = p->right;
}
}
}
return result;
}
};
相关文章推荐
- 第一,永远不要跟银行借钱;第二,永远不要向民间借贷;第三,量力而行(转)
- APP界面的设计风格 & 界面交互设计规范
- 实验三 进程调度模拟程序
- stand up meeting 12/7/2015
- 我的编程之路
- JavaScript DOM操作表格及样式
- Longest Palindromic Substring
- BeanFactory与FactoryBean
- ASP.NET运行时详解 生命周期入口分析
- 子集生成
- 杭电校赛2015‘11 1005
- POJ 1637 Sightseeing tour
- 排序算法(2)-直接插入排序
- homerHEVC代码阅读(23)——去方块滤波、SAO、熵编码的主要流程
- 黑马程序员——java基础之继承和多态
- CGI编程完全手册
- 彩票生成器--36选7(不重复)
- NAND: U-Boot BUG at mtdcore.c:20
- 忘记Orcale sys、system密码解决办法
- Linux常用命令大搜罗、大总结