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 了一下,算是投机取巧吧,之后写个从叶节点遍历的。

  • 相关阅读:
    centos 部署.NET CORE
    nginx 负载均衡
    graylog centos7 部署
    springboot 2.x centos 7.0 部署
    HashMap源代码阅读理解
    服务器安装redis
    java ---- gradle
    uboot-makefile总览
    makeFile
    Spring 推断构造方法
  • 原文地址:https://www.cnblogs.com/joelwang/p/10332378.html
Copyright © 2011-2022 走看看