zoukankan      html  css  js  c++  java
  • leetcode-剑指32-III-OK

    // language C with STL(C++)
    // 剑指32-III
    // https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof/
    
    
    /**
     * 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) {
        	vector<vector<int>> ans;
    
        	if(root == NULL)
        		return ans;	//空树
    
        	queue<TreeNode*> p;
        	queue<int> mid;
        	TreeNode* temp;
        	p.push(root);
        	int level =0;
        	int nowsize = 1;
        	int nextsize = 0;
        	while(nowsize!=0){
        		int i =0;
        		level++;
        		vector<int> linshi;
        		while(i<nowsize){
        			temp = p.front();
        			p.pop();
        			if(temp->left !=NULL){
        				p.push(temp->left);
        				nextsize++;
        			}
        			if(temp->right !=NULL){
        				p.push(temp->right);
        				nextsize++;
        			}
        			linshi.push_back(temp->val);
        			i++;
        		}	//本轮填装完毕
        		if(level%2 ==0){
                    reverse(linshi.begin(),linshi.end());
        		}
        		ans.push_back(linshi);
        		nowsize = nextsize;
        		nextsize = 0;
        	}
        	return ans;
        }
    };
    
  • 相关阅读:
    无线传感网3-1.目标物的覆盖技术
    无线传感网2-传感器布局方法
    JAVA 第二周课程总结
    2019春总结作业
    第十二周作业
    第十一周作业
    第十周
    第九周作业
    第八周作业
    第七周作业
  • 原文地址:https://www.cnblogs.com/gallien/p/14369730.html
Copyright © 2011-2022 走看看