32-2题:LeetCode102. Binary Tree Level Order Traversal二叉树层次遍历/分行从上到下打印二叉树

题目 

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]

考点


思路


代码

newcoder

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};
*/
class Solution {
public:
        vector<vector<int> > Print(TreeNode* pRoot) {
            //1.定义返回结果
            vector<vector<int>> ret;
            vector<int> subret;
            //2.入口检查
            if(!pRoot)
                return ret;
            
            //3.定义变量和队列
            int toBePrinted=1;//这一级还剩的节点数
            int NextLevel=0;//下一级的节点数
            queue<TreeNode*> queueTree; 
            int level=0;//当前的级数
            //4.放入根节点
            queueTree.push(pRoot);
            
            //5.循环
            while(!queueTree.empty())
            {
                TreeNode* cur=queueTree.front();
                subret.push_back(cur->val);
                queueTree.pop();
                
                //如果有左子节点
                if(cur->left)
                {
                    queueTree.push(cur->left);
                    NextLevel++;
                }
                
                //如果有右子节点
                if(cur->right)
                {
                    queueTree.push(cur->right);
                    NextLevel++;
                }
                
                toBePrinted--;
                if(!toBePrinted)
                {
                    level++;
                    toBePrinted=NextLevel;
                    NextLevel=0;
                    ret.push_back(subret);
                    subret.clear();
                }
            }
            
        return ret;
        }
    
};

leetcode 

/**
 * 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<vector<int>> levelOrder(TreeNode* root) {
        
        //1.返回容器
        vector<int> subret;
        vector<vector<int>> ret;
        
        //2.入口检查
        if(!root)
            return ret;
        
        //3.定义变量和队列
        //该层还剩打印的节点
        int remain=1;
        //下层的节点数
        int nextLevel=0;
        
        //树节点队列
        queue<TreeNode*> nodes;
        
        
        //将根节点入队列
        nodes.push(root);
        
        //4.将队列元素放入容器
        while(!nodes.empty())
        {
            //存储当前节点为队列的头部
            TreeNode* cur = nodes.front();
            //将当前节点塞入subret
            subret.push_back(cur->val);
            nodes.pop();
            remain--;
            
            if(cur->left)
            {
                nextLevel++;
                nodes.push(cur->left);
            }
            
            if(cur->right)
            {
                nextLevel++;
                nodes.push(cur->right);
            }
            
            //当前级元素全部读完
            if(!remain)
            {
                //将subret pushback进ret
                ret.push_back(subret);
                //将下一层的剩余元素个数更新                
                remain=nextLevel;
                //下一层的下一层初始化为0
                nextLevel=0;
                //清空subret
                subret.clear();
            }
            
        }
        
        //5.返回ret
        return ret;
        
         
        
    }
};

 


问题

1.queue

Member functions

(constructor)

empty

size

front

back

push

emplace 

Construct and insert element (public member function )

pop

swap 

Swap contents (public member function )

 

Non-member function overloads

relational operators

Relational operators for queue (function )

swap (queue) 

Exchange contents of queues (public member function )

queue只有push()。。。vector deque才用push_back()


2.二维vector遍历

#include<iostream>
#include<vector>

using namespace std;

int main()
{
	vector<vector<int>> ves;
	vector<int> a{ 1, 2, 3 };
	vector<int> b{ 2, 4, 5, 6 };

	ves.push_back(a);
	ves.push_back(b);
	for (auto it = ves.begin(); it != ves.end(); ++it){
		for (int i = 0; i < (*it).size(); ++i)
			cout << (*it)[i] << " " ;
	}
}

这题就用subret和ret来定义返回值,注意每层subret要清空。 

posted @ 2019-02-09 10:18 lightmare 阅读(...) 评论(...) 编辑 收藏