• [LeetCode] 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.

     

     

     

    1 BFS迭代,借用之前min depth和maxdepth of bianry tree 的框架

     1 struct newNode{
     2     TreeNode * node;
     3     int         dep; // current depth
     4 };
     5 
     6 class Solution {
     7     public:
     8         vector< vector<int> > levelOrder(TreeNode *root) {
     9             vector<vector<int> > result;
    10             if(root == NULL) return result;
    11 
    12             vector<int> levelRes;
    13             queue<newNode*> que;
    14             int curDep ;
    15 
    16             newNode *p = new newNode;
    17             p->node = root;
    18             curDep =  p->dep = 1;
    19             que.push(p);
    20 
    21             while( !que.empty())
    22             {
    23                 p= que.front();
    24                 que.pop();
    25 
    26                 TreeNode *pNode = p->node;
    27                 int dep = p->dep;
    28 
    29                 if(curDep == dep)
    30                 {
    31                     levelRes.push_back(pNode->val);
    32                 }
    33                 else
    34                 {
    35                     result.push_back(levelRes);
    36                     levelRes.clear();
    37                     levelRes.push_back(pNode->val);
    38                     curDep++;
    39                 }
    40 
    41                 if(pNode->left != NULL)
    42                 {
    43                     newNode *tmp = new newNode;
    44                     tmp->node = pNode->left;
    45                     tmp->dep = dep + 1;
    46                     que.push(tmp);
    47                 }
    48 
    49                 if(pNode->right != NULL)
    50                 {
    51                     newNode *tmp = new newNode;
    52                     tmp->node = pNode->right;
    53                     tmp->dep = dep + 1;
    54                     que.push(tmp);
    55                 }
    56 
    57             }
    58 
    59             // add the last level
    60             result.push_back(levelRes);
    61 
    62             return  result;
    63         }
    64 
    65 };

    2 DFS 也可以解决,就是在DFS中每次要传入一个dep,然后根据dep将数据加入到对应的vector中。

     DFS 递归(使用前序遍历框架)

     1 class Solution {
     2     private:
     3         vector<vector<int> > m_result;
     4     public:
     5         void solve(int dep, TreeNode *root)
     6         {   
     7             if (root == NULL)
     8                 return;
     9 
    10             if (m_result.size() > dep)
    11             {   
    12                 m_result[dep].push_back(root->val);
    13             }   
    14             else
    15             {   
    16                 vector<int> tmp;
    17                 tmp.push_back(root->val);
    18                 m_result.push_back(tmp);
    19             }   
    20 
    21             solve(dep + 1, root->left);
    22             solve(dep + 1, root->right);
    23         }   
    24 
    25         vector<vector<int> > levelOrder(TreeNode *root) {
    26             // Start typing your C/C++ solution below
    27             // DO NOT write int main() function
    28             m_result.clear();
    29             solve(0, root);
    30 
    31             return m_result;
    32         }   
    33 };

    DFS 迭代

     1 struct newNode{
     2     TreeNode * node;
     3     int         dep; // current depth
     4 };
     5 
     6 class Solution {
     7     private:
     8     //vector<vector<int> > retsult;
     9     public:
    10         vector< vector<int> > levelOrder(TreeNode *root) {
    11 
    12             vector<vector<int> > result;
    13             if(root == NULL) return result;
    14 
    15             vector<int> levelRes;
    16             stack<newNode*> s;
    17 
    18             newNode *p = new newNode;
    19             p->node = root;
    20             p->dep = 0;
    21             s.push(p);
    22 
    23             while (!s.empty()) {
    24                 p = s.top();
    25                 s.pop();
    26 
    27                 TreeNode *pNode = p->node;
    28                 int dep = p->dep;
    29 
    30                 if( dep + 1 > result.size() ) // new a vector
    31                 {
    32                     vector<int> tmp;
    33                     tmp.push_back(pNode->val);
    34                     result.push_back(tmp);
    35                 }
    36                 else // vector exists
    37                 {
    38                     result[dep].push_back(pNode->val);
    39                 }
    40           //注意先压入右子树,再压入左子树,弹出的时候先弹出左子树,在弹出右子树,实现前序遍历,Root->left->right
    41                 if(pNode->right != NULL)
    42                 {
    43                     newNode *tmp = new newNode;
    44                     tmp->node = pNode->right;
    45                     tmp->dep = dep + 1;
    46                     s.push(tmp);
    47                 }
    48 
    49                 if(pNode->left != NULL)
    50                 {
    51                     newNode *tmp = new newNode;
    52                     tmp->node = pNode->left;
    53                     tmp->dep = dep + 1;
    54                     s.push(tmp);
    55                 }
    56             }
    57             return result;
    58         }
    59 };

     3 双queue法

    class Solution {
        public:
            vector<vector<int> > levelOrder(TreeNode *root)
            {
                queue<TreeNode*> q1;
                queue<TreeNode*> q2;
                vector<vector<int> > res;
    
                if(root != NULL)
                {
                    q1.push(root);
                    res.push_back(vector<int> ());
                }
    
                int depth = 0;
                
    
                while(!q1.empty())
                {
                    TreeNode * p = q1.front();
                    q1.pop();
    
                    res[depth].push_back(p->val);
    
                    if(p->left)
                        q2.push(p->left);
                    if(p->right)
                        q2.push(p->right);
                    if(q1.empty() && !q2.empty())
                    {
                        swap(q1, q2);
                        depth ++;
                        res.push_back(vector<int> ());
                    }
                }   
                return res;
            }   
    };
  • 相关阅读:
    Python Requests-学习笔记(9)-错误与异常
    .NET C# 创建WebService服务简单的例子
    10个免费开源的JS音乐播放器插件
    7款高颜值HTML5播放器:让你的音乐有声有色
    Asp.net基于session实现购物车的方法
    ASP.NET用GridView控件实现购物车功能
    jquery鼠标跟随流体吸引特效
    jquery鼠标跟随特效
    JQUERY互动星空粒子背景效果
    jQuery插件库常用前端库引用地址
  • 原文地址:https://www.cnblogs.com/diegodu/p/3782396.html
Copyright © 2020-2023  润新知