zoukankan      html  css  js  c++  java
  • Leetcode 102. Binary Tree Level Order Traversal

    102. Binary Tree Level Order Traversal

    Total Accepted: 110104 Total Submissions: 327888 Difficulty: Easy

    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,null,null,15,7],

       3
       / 
      9  20
        /  
       15   7

    return its level order traversal as:

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

    思路:由顶至下的层序遍历,输出每一层不为空节点的值。

    代码:

    1.BFS

    代码中遍历一层时,用了3个指针:begin,end,cur,分别记录当前层的第1个非空节点,当前层最后1个非空节点,下一层最后1个非空节点。用BFS遍历。

     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>> res;
    14         if(root==NULL) return res;
    15         queue<TreeNode*> q;
    16         q.push(root);
    17         TreeNode *begin,*end=root,*cur=root;
    18         while(!q.empty()){
    19             vector<int> temp;
    20             begin=q.front();
    21             q.pop();
    22             while(begin!=end){
    23                 temp.push_back(begin->val);
    24                 if(begin->left){
    25                     q.push(begin->left);
    26                     cur=begin->left;
    27                 }
    28                 if(begin->right){
    29                     q.push(begin->right);
    30                     cur=begin->right;
    31                 }
    32                 begin=q.front();
    33                 q.pop();
    34             }
    35             temp.push_back(begin->val);
    36             if(begin->left){
    37                 q.push(begin->left);
    38                 cur=begin->left;
    39             }
    40             if(begin->right){
    41                 q.push(begin->right);
    42                 cur=begin->right;
    43             }
    44             res.push_back(temp);
    45             end=cur;
    46         }
    47         return res;
    48     }
    49 };

    类似的,作为一个技巧,可以先插入NULL到队列中,作为分层的标记:

     1 class Solution {
     2 public:
     3     vector<vector<int> > levelOrder(TreeNode* root) {
     4         vector<vector<int> > res;
     5         if(root==NULL) return res;
     6         vector<int> temp;
     7         queue<TreeNode*> q;
     8         q.push(root);
     9         q.push(NULL);
    10         TreeNode* cur;
    11         while(!q.empty()){
    12             cur=q.front();
    13             q.pop();
    14             while(cur){
    15                 temp.push_back(cur->val);
    16                 if(cur->left) q.push(cur->left);
    17                 if(cur->right) q.push(cur->right);
    18                 cur=q.front();
    19                 q.pop();
    20             }
    21             result.push_back(temp);
    22             if(q.size()>0){
    23                 temp.resize(0);
    24                 q.push(NULL);
    25             }
    26         }
    27         return res;
    28     }
    29 };

    2.DFS

     1 class Solution {
     2 public:
     3     vector<vector<int> > res;
     4 
     5     void DFS(TreeNode* root, int level)
     6     {
     7         if(root==NULL) return;
     8         if(res.size()==level){//一开始
     9             res.push_back(vector<int>());
    10         }
    11         res[level].push_back(root->val);
    12         DFS(root->left,level+1);
    13         DFS(root->right,level+1);
    14     }
    15     
    16     vector<vector<int> > levelOrder(TreeNode *root) {
    17         DFS(root, 0);
    18         return res;
    19     }
    20 };

    姐妹题:107. Binary Tree Level Order Traversal II

    做法可以相同,就是返回的结果反个顺序。

    这里注意一下:res是vector容器,则有

    res.begin() 返回一个迭代器,它指向容器c的第一个元素

    res.end() 返回一个迭代器,它指向容器c的最后一个元素的下一个位置

    res.rbegin() 返回一个逆序迭代器,它指向容器c的最后一个元素

    res.rend() 返回一个逆序迭代器,它指向容器c的第一个元素前面的位置

  • 相关阅读:
    C语言程序设计第一次作业
    C语言I博客作业09
    C语言I博客作业08
    C语言I博客作业07
    C语言I博客作业06
    C语言I博客作业05
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    学期总结
  • 原文地址:https://www.cnblogs.com/Deribs4/p/5626470.html
Copyright © 2011-2022 走看看