▶ 有关将一棵二叉树转化为二位表的题目,一模一样的套路出了四道题

▶ 第 102 题,简单的转化,[ 3, 9, 20, null, null, 15, 7 ] 转为 [ [ 15, 7 ] , [ 9, 20 ] , [ 3 ] ]

● 自己的代码,6 ms,先根序遍历,最快的解法算法与之相同

  1. class Solution
  2. {
  3. public:
  4. void visit(TreeNode* root, vector<vector<int>>& table, int level)
  5. {
  6. if (root == nullptr)
  7. return;
  8. if (table.size() <= level) // 首次到达该深度,建立新的一层
  9. table.push_back(vector<int>());
  10. visit(root->left, table, level + );
  11. table[level].push_back(root->val);
  12. visit(root->right, table, level + );
  13. return;
  14. }
  15. vector<vector<int>> levelOrderBottom(TreeNode* root)
  16. {
  17. vector<vector<int>> output;
  18. if (root == nullptr)
  19. return output;
  20. visit(root, output, );
  21. return output;
  22. }
  23. };

▶ 第 103 题,要求奇数行顺序输出,偶数行倒序输出,在第 102 题的基础上加个函数 reverse() 就行

● 自己的代码,4 ms,基于第 102 题,最快的解法算法与之相同

  1. class Solution
  2. {
  3. public:
  4. void visit(TreeNode* root, vector<vector<int>>& table, int level)
  5. {
  6. if (root == nullptr)
  7. return;
  8. if (table.size() <= level) // 首次到达该深度,建立新的一层
  9. table.push_back(vector<int>());
  10. visit(root->left, table, level + );
  11. table[level].push_back(root->val);
  12. visit(root->right, table, level + );
  13. return;
  14. }
  15. vector<vector<int>> levelOrderBottom(TreeNode* root)
  16. {
  17. vector<vector<int>> output;
  18. if (root == nullptr)
  19. return output;
  20. visit(root, output, );
  21. for (int i = ; i < output.size(); i += )
  22. reverse(output[i].begin(), output[i].end());
  23. return output;
  24. }
  25. };

▶ 第 107 题,要求倒序输出,还是在第 102 题 的基础上加个函数 reverse()  就行

● 自己的代码,5 ms,最快的解法算法与之相同,但是使用的数据结构是 list,新建一层用的是函数 resize()

  1. class Solution
  2. {
  3. public:
  4. void visit(TreeNode* root, vector<vector<int>>& table, int level)
  5. {
  6. if (root == nullptr)
  7. return;
  8. if (table.size() <= level) // 首次到达该深度,建立新的一层
  9. table.push_back(vector<int>());
  10. visit(root->left, table, level + );
  11. table[level].push_back(root->val);
  12. visit(root->right, table, level + );
  13. return;
  14. }
  15. vector<vector<int>> levelOrderBottom(TreeNode* root)
  16. {
  17. vector<vector<int>> output;
  18. if (root == nullptr)
  19. return output;
  20. visit(root, output, );
  21. reverse(output.begin(), output.end());
  22. return output;
  23. }
  24. };

▶ 第 637 题,在原来转化为二维表的基础上计算每一层的平均数,压成一维表输出

● 自己的代码,17 ms,基于第 102 题的遍历函数,输出二维表以后再一层一层计算平均数。最快的解法算法与之相同,但不再将树转化为表以后再算平均值,而是在遍历树的同时维护一个元素个数表和一个平均值表,每遍历一个非空结点就更新该层的元素个数和平均值

  1. class Solution
  2. {
  3. public:
  4. void visit(TreeNode* root, vector<vector<int>>& table, int level)
  5. {
  6. if (root == nullptr)
  7. return;
  8. if (table.size() <= level) // 首次到达该深度,建立新的一层
  9. table.push_back(vector<int>());
  10. visit(root->left, table, level + );
  11. table[level].push_back(root->val);
  12. visit(root->right, table, level + );
  13. return;
  14. }
  15. vector<double> averageOfLevels(TreeNode* root)
  16. {
  17. vector<double> output;
  18. if (root == nullptr)
  19. return output;
  20. int i, j;
  21. double sum;
  22. vector<vector<int>> table;
  23. visit(root, table, );
  24. for (i = ; i < table.size(); i++)
  25. {
  26. for (j = , sum = 0.0f; j < table[i].size(); j++)
  27. sum += table[i][j];
  28. output.push_back(sum / table[i].size());
  29. }
  30. return output;
  31. }
  32. };

102. Binary Tree Level Order Traversal + 103. Binary Tree Zigzag Level Order Traversal + 107. Binary Tree Level Order Traversal II + 637. Average of Levels in Binary Tree的更多相关文章

  1. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  2. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  3. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  4. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  5. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  6. LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

  7. LeetCode - 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  8. [LeetCode&Python] Problem 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  9. 637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值

    [抄题]: Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

随机推荐

  1. MySQL —— 基本查询方法

    MySQL —— 简单查询与按条件查询 在MySQL中从数据表中查询数据的基本语句时select语句.  select语句基本语法格式:      select 查询内容       from 表名  ...

  2. HDU-1532 Drainage Ditches (最大流,EK算法模板)

    题目大意:最大流的模板题...源点是0,汇点是n-1. 代码如下: # include<iostream> # include<cstdio> # include<cma ...

  3. javascript中new Date()会存在偏差一小时的bug

    事件回顾: 因为我们的产品会有与时间转换这部分,并且流量主要集中在小程序. emmm~  获取用户出生的年/月/日/时  我们和后台协商的是换算用户选择后的时间为  年/月/日/时/分/秒  所以我们 ...

  4. 使用java.net.URLConnection发送http请求

    首先,这个需要一点HTTP基础,可以先看个书了解下,我看的<http权威指南>的前4章,后面道行不够看不下去. 然后我们的是java.net的接口: 几个类的API: package co ...

  5. JDK5的新特性:泛型、可变参数、静态导入

    数组可以在创建的时候就指定存放的数据类型,这样放入不同类型的时候就会发生编译错误. 而集合却可以存储多种不同类型,这样的话如果是遍历的时候在集合中装了好多不同的数据类型的时候,十分容易发生类型转换错误 ...

  6. 理解 Socket

    原文链接 题外话 前几天和朋友聊天,朋友问我怎么最近不写博客了,一个是因为最近在忙着公司使用的一些控件的开发,浏览器兼容性搞死人 但主要是因为这段时间一直在看html5的东西,看到web socket ...

  7. CI框架------codeIgniter

    之前学习了thinkphp,学完之后印象不太深刻,在网上询问了一下,他们都说多学几个框架,以后可以自己写框架. 于是自己就放下thinkphp,下定决心再学一个,于是又从网上看了几个框架,综合比较了一 ...

  8. 用eclipse来运行带参数的命令行程序,配置命令行程序的参数

    以上从网上找了点资料:右键点主类名 --> 运行--> 打开运行对话框--> Main(主类)--> 右边Arguments(参数) 点他以后然后在下面Program argu ...

  9. python set集合运算(交集,并集,差集,对称差集)

    1>交集>>> x={1,2,3,4}>>> y={3,4,5,6}>>> xset([1, 2, 3, 4])>>> y ...

  10. RabbitMQ内存爆出问题解决思路

    http://www.bubuko.com/infodetail-2121050.html RabbitMQ升级到3.6.1版本后,随着业务和系统功能的增加,出现RabbitMQ内存陡增直至服务宕掉的 ...