zoukankan      html  css  js  c++  java
  • [LeetCode] Binary Tree Level Order Traversal II

    iven a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

    For example:
    Given binary tree {3,9,20,#,#,15,7},

        3
       / 
      9  20
        /  
       15   7
    

    return its bottom-up level order traversal as:

    [
      [15,7],
      [9,20],
      [3]
    ]

    思路:思路和Binary Tree Level Order Traversal相同,在最后的时候reverse.

     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> > result;
    14         if (root == NULL) return result;
    15         
    16         vector<int> level;
    17         
    18         queue<TreeNode* > q;
    19         q.push(root);
    20         q.push(0);
    21         
    22         while(!q.empty()) {
    23             TreeNode* p = q.front();
    24             q.pop();
    25             if (p != NULL) {
    26                 level.push_back(p->val);
    27                 if (p->left) q.push(p->left);
    28                 if (p->right) q.push(p->right);
    29             } else {
    30                 result.push_back(level);
    31                 level.clear();
    32                 
    33                 if (!q.empty())
    34                     q.push(0);
    35             }
    36         }
    37         
    38         reverse(result.begin(), result.end());
    39         
    40         return result;
    41     }
    42 };
  • 相关阅读:
    MobileNet V1 V2
    异常检测 与 One Class SVM
    异常检测
    图像分割
    1x1卷积核的作用
    迁移学习
    python
    图像分割
    图像分割
    Nagios
  • 原文地址:https://www.cnblogs.com/vincently/p/4229779.html
Copyright © 2011-2022 走看看