二叉树的前序遍历(不用递归)

题目描述

 

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?

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> preorderTraversal(TreeNode *root) {
13             vector<int> vec;
14             stack<TreeNode *> s;
15             if (root == NULL)
16             {
17                 return vec;
18             }
19             s.push(root);
20             while (!s.empty())
21             {
22                 TreeNode *Cur = s.top();
23                 s.pop();
24                 vec.push_back(Cur->val);
25                 if (Cur->right != NULL)
26                     s.push(Cur->right);
27                 if (Cur->left != NULL)
28                     s.push(Cur->left);
29             }
30             return vec;
31     }
32 };

 

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> postorderTraversal(TreeNode *root) {//后序遍历
13         vector<int> vec;
14     stack<TreeNode *> s;
15     TreeNode *p = root;
16 
17     if (root == NULL)
18         return vec;
19 
20     s.push(root);
21 
22     while (!s.empty())
23     {
24         while (p->left != NULL)
25         {
26             p = p->left;
27             s.push(p);
28         }
29         if (p->right == NULL)
30         {
31             TreeNode *cur = s.top();
32             s.pop();
33             vec.push_back(cur->val);
34             if (!s.empty())
35                 p = s.top();
36             if (p->left != NULL)
37             {
38                 if (p->left->val == cur->val)
39                     p->left = NULL;
40             }
41             if (p->right != NULL)
42             {
43                 if (p->right->val == cur->val)
44                     p->right = NULL;
45             }
46 
47             if (p->right != NULL)
48             {
49                 p = p->right;
50                 s.push(p);
51             }
52         }
53         else
54         {
55             p = p->right;
56             s.push(p);
57         }
58         
59     }
60     return vec;
61         
62     }
63 };

 

/**
 * 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 postOrder(TreeNode *root,vector<int>&vec){//后序、、递归
        if(root != NULL){
            postOrder(root->left,vec);
            postOrder(root->right,vec);
            vec.push_back(root->val);
        }
    }
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int>vec;
        postOrder(root,vec);
        return vec;      
         
    }
};

 

posted @ 2016-07-16 17:10  hhboboy  阅读(573)  评论(0编辑  收藏  举报