鱼C论坛

 找回密码
 立即注册
查看: 2330|回复: 0

[学习笔记] leetcode 102. Binary Tree Level Order Traversal

[复制链接]
发表于 2019-10-2 10:16:51 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2019-10-3 05:43 编辑
  1. Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

  2. For example:
  3. Given binary tree [3,9,20,null,null,15,7],
  4.     3
  5.    / \
  6.   9  20
  7.     /  \
  8.    15   7
  9. return its level order traversal as:
  10. [
  11.   [3],
  12.   [9,20],
  13.   [15,7]
  14. ]
复制代码

  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.         // return BFS(root);  
  14.         vector<vector<int>> ans;
  15.         DFS(root,0, ans);
  16.         return ans;
  17.     }

  18. private:
  19.     vector<vector<int>> BFS(TreeNode* root){
  20.         vector<vector<int>> ans;
  21.         if(root == NULL) return ans;
  22.         vector<TreeNode*> cur, next;
  23.         cur.push_back(root);
  24.         
  25.         while(!cur.empty()){
  26.             ans.push_back({});
  27.             for(TreeNode* node : cur){
  28.                 ans.back().push_back(node->val);
  29.                
  30.                 if(node->left != NULL) next.push_back(node->left);
  31.                 if(node->right != NULL) next.push_back(node->right);
  32.             }
  33.             
  34.             cur.swap(next);
  35.             next.clear();
  36.         }
  37.         
  38.         return ans;
  39.     }
  40.    
  41.     void DFS(TreeNode* root , int depth, vector<vector<int>>& ans){
  42.         if(root == NULL) return;
  43.         while(ans.size() <= depth) ans.push_back({});
  44.         DFS(root->left,depth+1, ans);
  45.         DFS(root->right,depth+1,ans);
  46.         ans[depth].push_back(root->val);
  47.     }
  48. };
复制代码


optimized DFS

  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.         // return BFS(root);  
  14.         vector<vector<int>> ans;
  15.         if(!root) return ans;
  16.         DFS(root,0, ans);
  17.         return ans;
  18.     }

  19. private:
  20.     void DFS(TreeNode* root , int depth, vector<vector<int>>& ans){
  21.         if(root != NULL) {
  22.             if(ans.size() > depth){
  23.                 ans[depth].push_back(root->val);
  24.             }else{
  25.                 ans.push_back(vector <int>{root->val});
  26.             }
  27.             DFS(root->left, depth+1, ans);
  28.             DFS(root->right,depth+1, ans);
  29.         }
  30.     }
  31. };
复制代码

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 17:18

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表