leetcode OJ -Binary Tree Preorder Traversal(2014.1.20)
2014-04-19 23:13
387 查看
递归:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void preorder(TreeNode *root,vector<int> &path)
{
if(root!=NULL)
{
path.push_back(root->val);
preorder(root->left,path);
preorder(root->right,path);
}
}
vector<int> preorderTraversal(TreeNode *root) {
vector<int> path;
preorder(root,path);
return path;
}
};
非递归 :
/**
* Definition for binary tree
* 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> path;
stack<TreeNode*> stk;
if(root==NULL) return path;
stk.push(root);
TreeNode *cur=NULL;
while(!stk.empty())
{
cur=stk.top();
path.push_back(cur->val);
stk.pop();
if(cur->right!=NULL){
stk.push(cur->right);
cur->right==NULL;
}
if(cur->left!=NULL){
stk.push(cur->left);
cur->left=NULL;
}
}
}
};
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void preorder(TreeNode *root,vector<int> &path)
{
if(root!=NULL)
{
path.push_back(root->val);
preorder(root->left,path);
preorder(root->right,path);
}
}
vector<int> preorderTraversal(TreeNode *root) {
vector<int> path;
preorder(root,path);
return path;
}
};
非递归 :
/**
* Definition for binary tree
* 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> path;
stack<TreeNode*> stk;
if(root==NULL) return path;
stk.push(root);
TreeNode *cur=NULL;
while(!stk.empty())
{
cur=stk.top();
path.push_back(cur->val);
stk.pop();
if(cur->right!=NULL){
stk.push(cur->right);
cur->right==NULL;
}
if(cur->left!=NULL){
stk.push(cur->left);
cur->left=NULL;
}
}
}
};
相关文章推荐
- 【LeetCode OJ】Binary Tree Preorder Traversal
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ:Binary Tree Preorder Traversal
- LeetCode OJ - Binary Tree Preorder Traversal
- LeetCode OJ 之 Binary Tree Preorder Traversal (二叉树的前序遍历)
- [LeetCode OJ] Binary Tree Preorder Traversal 解题报告
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ --问题与解答 Binary Tree Preorder Traversal
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal(从前序以及中序遍历结果中构造二叉树)
- leetcode OJ -Binary Tree Postorder Traversal(2014.1.20)
- LeetCode OJ - Binary Tree Preorder Traversal
- LeetCode OJ:Construct Binary Tree from Preorder and Inorder Traversal
- 【LEETCODE OJ】Binary Tree Preorder Traversal
- 【LeetCode OJ】Construct Binary Tree from Preorder and Inorder Traversal
- LeetCode OJ--Binary Tree Preorder Traversal
- LeetCode OJ 105. Construct Binary Tree from Preorder and Inorder Traversal
- [LeetCode OJ]Binary Tree Preorder Traversal
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
- LeetCode OJ - Construct Binary Tree from Preorder and Inorder Traversal
- 144. Binary Tree Preorder Traversal