Binary Tree Traversal

1、Preorder Traversal

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?

解题:前序遍历首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树。这里依然使用栈实现。先访问根结点,再将右结点、左结点压到栈中,根据栈先进后出的特性,会先遍历左子树,再遍历右子树。

/**
 * 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> ans;
        stack<TreeNode*> que;
        TreeNode *tmp;
        if(root != NULL){
            
            que.push(root);
            while(!que.empty())
            {
                tmp = que.top();
                que.pop();
                ans.push_back(tmp->val);
                
                if(tmp->right != NULL)
                    que.push(tmp->right);
                if(tmp->left != NULL)
                    que.push(tmp->left);
            }
        }
        return ans;
    }
};

 2、 Inorder Traversal

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [1,3,2].

解题:中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树。在遍历左、右子树时,仍然先遍历左子树,然后访问根结点,最后遍历右子树。这里依然使用栈实现。

/**
 * 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> inorderTraversal(TreeNode *root) {
        vector<int> ans;
        stack<TreeNode*> stk;
        TreeNode* tmp = root;
        while(tmp != NULL || !stk.empty())
        {
            while(tmp != NULL)
            {
                stk.push(tmp);
                tmp = tmp->left;
            }
            if(!stk.empty())
            {
                tmp = stk.top();
                ans.push_back(tmp->val);
                stk.pop();
                tmp = tmp->right;
            }
        }
        return ans;
    }
};

3、Postorder Traversal

For example:
Given binary tree {1,#,2,3},

   1
    \
     2
    /
   3

return [3,2,1].

解题:后序遍历首先遍历左子树,然后遍历右子树,最后访问根结点。算法处理过程:先迭代并压栈至左子树最左结点,取出栈顶元素,判断是否具有右儿子(或者其右儿子刚刚被访问),如果条件为真,便访问该结点,否则对其右儿子进行后序遍历。

/**
 * 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> postorderTraversal(TreeNode *root) {
        vector<int> ans;
        stack<TreeNode*> stk; 
        TreeNode *pre,*tmp = root;
        while(tmp != NULL || !stk.empty()){
            while(tmp != NULL) {
                stk.push(tmp);
                tmp = tmp->left;
            }
            tmp = stk.top();
            if(tmp->right == NULL || tmp->right == pre){
                ans.push_back(tmp->val);
                stk.pop();
                pre = tmp;
                tmp = NULL;
            }
            else {
                tmp = tmp->right;
            }
        }
        return ans;
    }
};

 

 

posted on 2014-05-03 15:28  bbking  阅读(202)  评论(0编辑  收藏  举报

导航