您的位置:首页 > 其它

Binary Tree Traversal(Preorder, Inorder, Postorder )

2016-09-19 10:41 423 查看
Given a binary tree, return the preorder traversal of its nodes’ values.

For example:

Given binary tree {1,#,2,3},

1

\

2

/

3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

144. Binary Tree Preorder Traversal

94. Binary Tree Inorder Traversal

145. Binary Tree Postorder Traversal

递归:

//Recursive  preorder  Runtime: 3 ms
/**
* 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;
if(root==NULL){
return res;
}
if(root){
res.push_back(root->val);
}
if(root->left){
vector<int> leftv=preorderTraversal(root->left);
res.insert(res.end(),leftv.begin(),leftv.end());
}
if(root->right){
vector<int> rightv=preorderTraversal(root->right);
res.insert(res.end(),rightv.begin(),rightv.end());
}
return res;
}
};


//Recursive  inorder Runtime: 0 ms
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL){
return res;
}
if(root->left){
vector<int> leftv=inorderTraversal(root->left);
res.insert(res.end(),leftv.begin(),leftv.end());
}
res.push_back(root->val);
if(root->right){
vector<int> rightv=inorderTraversal(root->right);
res.insert(res.end(),rightv.begin(),rightv.end());
}
return res;
}
};


//Recursive  postorder  Runtime: 0 ms
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
if(root==NULL){
return res;
}
if(root->left){
vector<int> leftv=postorderTraversal(root->left);
res.insert(res.end(),leftv.begin(),leftv.end());
}
if(root->right){
vector<int> rightv=postorderTraversal(root->right);
res.insert(res.end(),rightv.begin(),rightv.end());
}
res.push_back(root->val);
return res;
}
}


迭代:

//iterative    preorder    Runtime: 0 ms
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> prestack;
while(root||!prestack.empty()){
while(root){
prestack.push(root);
res.push_back(root->val);
root=root->left;
}
root=prestack.top();
prestack.pop();
root=root->right;
}
return res;
}
};


// iterative inorder Runtime: 3 ms
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> prestack;
while(root||!prestack.empty()){
while(root){
prestack.push(root);

root=root->left;
}
root=prestack.top();
res.push_back(root->val);
prestack.pop();
root=root->right;
}
return res;
}
};


//interative postorder Runtime: 0 ms
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
stack<TreeNode*> prestack;
while(root||!prestack.empty()){
while(root){
prestack.push(root);
res.insert(res.begin(),root->val);
root=root->right;
}
root=prestack.top();
prestack.pop();
root=root->left;
}
return res;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: