zoukankan      html  css  js  c++  java
  • leetcode107 二叉树的层次遍历2

     1 /*
     2  * @lc app=leetcode.cn id=107 lang=cpp
     3  *
     4  * [107] 二叉树的层次遍历 II
     5  */
     6 
     7 // @lc code=start
     8 /**
     9  * Definition for a binary tree node.
    10  * struct TreeNode {
    11  *     int val;
    12  *     TreeNode *left;
    13  *     TreeNode *right;
    14  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
    15  * };
    16  */
    17 class Solution {
    18 public:
    19 //和102一样的思路,最后加一个翻转
    20     vector<vector<int>> levelOrderBottom(TreeNode* root) {
    21         if(!root) return {};
    22         vector<vector<int>> res;
    23         queue<TreeNode*> q;
    24 
    25         q.push(root);
    26         while(!q.empty()){
    27             int len=q.size();
    28             vector<int> tmp;
    29             for(int i=0;i<len;i++){
    30                 TreeNode* node=q.front();
    31                 q.pop();
    32                 tmp.push_back(node->val);
    33                 if(node->left) q.push(node->left);
    34                 if(node->right) q.push(node->right);
    35             }
    36             res.push_back(tmp);
    37 
    38         }
    39         reverse(res.begin(),res.end());
    40         return res;
    41     }
    42 };
    43 // @lc code=end
  • 相关阅读:
    毫秒倒计时小Demo
    css3 翻转
    canvas
    html5
    css3
    一些免费的svn空间(SVN代码托管)
    Xcode 6制作动态及静态Framework
    ios 动态执行的代码
    ios nsarray对象问题
    iOS xcode 编译选项 architecture(cup架构问题)
  • 原文地址:https://www.cnblogs.com/yaodao12/p/13939290.html
Copyright © 2011-2022 走看看