首页 > 代码库 > Leetcode:Binary_Tree_Level_Order_Traversal_II

Leetcode:Binary_Tree_Level_Order_Traversal_II

一、     题目

给定一个二叉树,由自底向上的层次顺序遍历返回其节点的值。 (即,由左到右,一层一层地从叶到根目录)。

例:       3              输出:[  [15,7],

        /   \                            [9,20]

    9      20                        [3]

          /     \                    ]

       15      7

二、     分析

       看到题目首先想到层次遍历,每一层从左到右保存到一个队列中,并依次把元素值保存到一个一维数组,最后遍历完一层时无非是将结果保存至一个二维数组,可分为以下步骤:

     1、把根节点保存至队列,并将count=1;

     2、遍历该层(上一层已保存至队列中的元素)同时将值保存到一维数组,同时判断各个节点有无左右子节点,若有则nextcount的值加一并入队列;直到遍历完成

     3、将该层的一维数组保存至二维数组,并将count=nextcount以便供下次使用,若此时队列不空则(2)

     4、当整个二叉树遍历结束时,反转二维数组,得结果


/**
 * 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) {
        vector<vector<int>> ans;
		if(root==NULL) return ans;
		vector<int> eac;
		queue<TreeNode *> que;
		que.push(root);
		int count = 1;
		
		while(!que.empty()){
			eac.clear();
			int nextcount = 0;
			for(int i=0;i<count;i++){
				TreeNode *temp = que.front();
				que.pop();
				
				eac.push_back(temp->val);
				if(temp->left){
					nextcount++;
					que.push(temp->left);
				} 
				if(temp->right){
					nextcount++;
					que.push(temp->right);
				} 
			}
			count = nextcount;
			ans.push_back(eac);
		} 
		reverse(ans.begin(),ans.end());
		return ans;
    }
};


/**
 * 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>> ans;
        if(root == NULL) return ans;
        
        vector<int> eac;
        queue<TreeNode *> que;
        queue<TreeNode *> que2;   // extra space
        
        que.push(root);
        while(!que.empty()){
            TreeNode *temp = que.front();
            que.pop();
            
            if(temp != NULL){
                eac.push_back(temp->val);
                if(temp->left) que2.push(temp->left);
                if(temp->right) que2.push(temp->right);
            }
            
            if(que.empty()){      // one row end
                ans.push_back(eac);
                eac.clear();
                swap(que, que2);
            }
        }
        
        reverse(ans.begin(), ans.end());    // reverse vector
    }
};

/**
 * 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
        list<vector<int> > retTemp;

        queue<TreeNode *> trace;
        trace.push(root);
        trace.push(NULL);
        
        vector<int> curLevel;
        while(true) {
            TreeNode *cur = trace.front();
            trace.pop();
            if(cur) {
                curLevel.push_back(cur->val);
                if(cur->left) trace.push(cur->left);
                if(cur->right) trace.push(cur->right);
            } else {
                if(curLevel.size()) {
                    retTemp.push_front(curLevel);
                    curLevel.erase(curLevel.begin(),curLevel.end());
                    trace.push(NULL);
                } else {
                    break;
                }
            }
        }
        
        vector<vector<int> > ret;
        for(list<vector<int> >::iterator it = retTemp.begin(); it != retTemp.end(); ++it) {
            ret.push_back(*it);
        }
        return ret;
    }
};



 

Leetcode:Binary_Tree_Level_Order_Traversal_II