LeetCode_Binary Tree Level Order Traversal II

A过了,但是是那种BT解法,明天再想个正解

/**
 * 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<vector<int> > levelOrderBottom(TreeNode *root) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<vector<int>> result, result1;
        vector<int> tempR;
        queue<TreeNode *> a,b,c;
        if(root == NULL) return result;
        TreeNode *current;
        a.push(root);
        while(!a.empty()){
           
           tempR.clear();
           while(!a.empty())
           {
                current = a.front();
                a.pop();
                tempR.push_back(current->val);
                if(current->left)
                      b.push(current->left);
                if(current->right)
                      b.push(current->right);
           }
           result.push_back(tempR);
           c = a;
           a = b;
           b = c;
        }        
        
        for(int i =result.size()-1;i>=0;i--)
            result1.push_back(result[i]);
        return result1;
    }
};

 

posted @ 2013-04-10 00:18  冰点猎手  阅读(117)  评论(0编辑  收藏  举报