zoukankan      html  css  js  c++  java
  • 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]
    ]
    

    confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

     

    Analyse: 

    1. Recursion: If the level does not exist, create it and then push corresponding value into it. 

    Runtime: 4ms.

     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> >result;
    14         traverse(root, 0, result);
    15         return result;
    16     }
    17     void traverse(TreeNode* root, int level, vector<vector<int> >& result){
    18         if(!root) return;
    19         if(level == result.size()) //the level does not exist and need to create it
    20             result.push_back(vector<int> ());
    21         
    22         result[level].push_back(root->val);
    23         traverse(root->left, level + 1, result);
    24         traverse(root->right, level + 1, result);
    25     }
    26 };

    2. Iteration: Using queue to store nodes. When poping the first node, we need to add its children(child) to the queue. Then keep poping until the queue is empty.

        Runtime: 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> > result;
    14         if(!root) return result;
    15         
    16         queue<TreeNode* > qu;
    17         qu.push(root);
    18         while(!qu.empty()){
    19             int n = qu.size();
    20             vector<int> level; //store the visited nodes in current level
    21             while(n--){
    22                 TreeNode* temp = qu.front(); //pop the first node in the queue
    23                 level.push_back(temp->val);
    24                 qu.pop();
    25                 if(temp->left) qu.push(temp->left); //add its children(child)
    26                 if(temp->right) qu.push(temp->right);
    27             }
    28             result.push_back(level);
    29         }
    30         return result;
    31     }
    32 };
  • 相关阅读:
    怎么用代码弹回 UITableView 中左滑出来的删除按钮
    android 利用 aapt 解析 apk 得到应用名称 包名 版本号 权限等信息
    Missy
    html5 websocket + node.js 实现网页聊天室
    android 代码混淆示例
    android volley 发送 POST 请求
    android viewpager 拿到当前显示的 fragment 的实例
    android actionbar viewpager 实现类微信主界面布局
    (转)初学Git及简单搭建git服务器和客户端
    error: Cannot find OpenSSL's <evp.h> Mac
  • 原文地址:https://www.cnblogs.com/amazingzoe/p/4681372.html
Copyright © 2011-2022 走看看