zoukankan      html  css  js  c++  java
  • leetcode 107.Binary Tree Level Order Traversal II 二叉树的层次遍历 II

    相似题目:

    102 103 107

     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         if(root==NULL) return {};
    14         queue<TreeNode*> q;
    15         TreeNode* front;
    16         q.push(root);
    17         vector<vector<int>> res;
    18         while(!q.empty()){
    19             vector<int> onelevel;
    20             for(int i=q.size();i>0;i--){
    21                 front=q.front();
    22                 q.pop();
    23                 if(front->left)
    24                     q.push(front->left);
    25                 if(front->right)
    26                     q.push(front->right);
    27                 onelevel.push_back(front->val);
    28             }
    29             res.push_back(onelevel);
    30         }
    31         reverse(res.begin(),res.end());
    32         return res;
    33     }
    34 };

     其实这个解答只是reverse 了一下,算是投机取巧吧,之后写个从叶节点遍历的。

  • 相关阅读:
    h5 穿透滚动
    Safari 导航栏
    浮层滚动问题
    兼容性常规检测
    CORS
    Webpack 一些概念
    01 离散时间信号的时域表示
    03 信道容量
    01 MATLAB基本概念
    02 字斟句酌
  • 原文地址:https://www.cnblogs.com/joelwang/p/10332378.html
Copyright © 2011-2022 走看看