• LeetCode Binary Tree Level Order Traversal II (二叉树颠倒层序)


    题意:从左到右统计将同一层的值放在同一个容器vector中,要求上下颠倒,左右不颠倒。

    思路:广搜逐层添加进来,最后再反转。

     1 /**
     2  * Definition for a binary tree node.
     3  * struct TreeNode {
     4  *     int val;
     5  *     TreeNode *left;
     6  *     TreeNode *right;
     7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     8  * };
     9  */
    10 class Solution {
    11 public:
    12     vector< vector<int> > levelOrderBottom(TreeNode* root) {
    13         vector< vector<int> >   ans;
    14         if(!root)   return ans;
    15         vector<int> tmp;
    16         deque<TreeNode * >    que;
    17         que.push_back(root);
    18         while(!que.empty())     //广搜
    19         {
    20             int siz= que.size();
    21             tmp.clear();
    22             for(int i=0; i<siz; i++)
    23             {
    24                 TreeNode* v=que.front();que.pop_front();
    25                 tmp.push_back(v->val);
    26                 if(v->left)     que.push_back(v->left);
    27                 if(v->right)    que.push_back(v->right);
    28             }
    29             ans.push_back(tmp);
    30         }
    31         reverse(ans.begin(), ans.end());
    32         return ans;
    33     }
    34 };
    AC代码
  • 相关阅读:
    react-document-title
    react-router
    redux-saga 异步流
    redux
    redux-thunk
    react-router-redux
    [翻译] ClockView 时钟
    [翻译] MZTimerLabel 用作秒表或者倒计时
    [翻译] MCProgressView 使用自定义图片做进度显示
    [翻译] ADPopupView 触摸弹出视窗
  • 原文地址:https://www.cnblogs.com/xcw0754/p/4619796.html
Copyright © 2020-2023  润新知