zoukankan      html  css  js  c++  java
  • LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

    Binary Tree Level Order Traversal

      题目链接

      题目要求:

      Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

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

        3
       / 
      9  20
        /  
       15   7

      return its level order traversal as:

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

      这道题利用宽度优先搜索就可以了,具体程序(8ms)如下:

     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>> levelOrder(TreeNode* root) {
    13         vector<vector<int>> retVec;
    14         if(!root)
    15             return retVec;
    16 
    17         retVec.push_back(vector<int>{root->val});
    18         queue<TreeNode *> que;
    19         que.push(root);
    20         while(true)
    21         {
    22             vector<int> vec;
    23             queue<TreeNode *> q;
    24             while(!que.empty())
    25             {
    26                 TreeNode *tree = que.front();
    27                 que.pop();
    28                 if(tree->left)
    29                 {
    30                     vec.push_back((tree->left)->val);
    31                     q.push(tree->left);
    32                 }
    33                 if(tree->right)
    34                 {
    35                     vec.push_back((tree->right)->val);
    36                     q.push(tree->right);
    37                 }
    38             }
    39             
    40             if(!q.empty())
    41             {
    42                 que = q;
    43                 retVec.push_back(vec);
    44             }
    45             else
    46                 break;
    47         }
    48         
    49         return retVec;
    50     }
    51 };

    Binary Tree Level Order Traversal II

      题目链接

      题目要求:

      Given 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]
    ]

      这道题基本更上边的题目一样,我们只需要将上题的结果retVec最后再反转一下就可以了,即添加如下一行即可:

    1 reverse(retVec.begin(), retVec.end());

      这样的程序只要8ms,但下边基本一样的程序却要64ms:

     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>> retVec;
    14         if(!root)
    15             return retVec;
    16 
    17         retVec.insert(retVec.begin(), vector<int>{root->val});
    18         queue<TreeNode *> que;
    19         que.push(root);
    20         while(true)
    21         {
    22             vector<int> vec;
    23             queue<TreeNode *> q;
    24             while(!que.empty())
    25             {
    26                 TreeNode *tree = que.front();
    27                 que.pop();
    28                 if(tree->left)
    29                 {
    30                     vec.push_back((tree->left)->val);
    31                     q.push(tree->left);
    32                 }
    33                 if(tree->right)
    34                 {
    35                     vec.push_back((tree->right)->val);
    36                     q.push(tree->right);
    37                 }
    38             }
    39             
    40             if(!q.empty())
    41             {
    42                 que = q;
    43                 retVec.insert(retVec.begin(), vec);
    44             }
    45             else
    46                 break;
    47         }
    48             
    49         return retVec;
    50     }
    51 };
    View Code
  • 相关阅读:
    C#委托
    资源推荐 五个常用MySQL图形化管理工具
    C#数组
    虚方法与多态
    How to connect to MySQL database from Visual Studio VS2010 – problems with NET connectors
    2021秋软工实践第二次结对编程作业
    2021秋软工实践第一次个人编程作业
    低维数据可视化
    2021秋季软件工程实践总结
    2021秋软工实践第一次结对编程作业
  • 原文地址:https://www.cnblogs.com/xiehongfeng100/p/4631447.html
Copyright © 2011-2022 走看看