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

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

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

 class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
return output;
}
};

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

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

 class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
for (int i = ; i < output.size(); i += )
reverse(output[i].begin(), output[i].end());
return output;
}
};

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

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

 class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<vector<int>> levelOrderBottom(TreeNode* root)
{
vector<vector<int>> output;
if (root == nullptr)
return output;
visit(root, output, );
reverse(output.begin(), output.end());
return output;
}
};

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

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

 class Solution
{
public:
void visit(TreeNode* root, vector<vector<int>>& table, int level)
{
if (root == nullptr)
return;
if (table.size() <= level) // 首次到达该深度,建立新的一层
table.push_back(vector<int>());
visit(root->left, table, level + );
table[level].push_back(root->val);
visit(root->right, table, level + );
return;
}
vector<double> averageOfLevels(TreeNode* root)
{
vector<double> output;
if (root == nullptr)
return output;
int i, j;
double sum;
vector<vector<int>> table;
visit(root, table, );
for (i = ; i < table.size(); i++)
{
for (j = , sum = 0.0f; j < table[i].size(); j++)
sum += table[i][j];
output.push_back(sum / table[i].size());
}
return output;
}
};

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. Python str 与 bytes 类型 之间的转换

    bytes:字节数组,通常用它可以描述 “一个字符串”,只不过该字符串是  “bytes类型”,所以容易与str类型混淆,他们二者之间的转换: https://blog.csdn.net/lanchu ...

  2. 巴什博奕——hdu2149

    #include <bits/stdc++.h> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f ...

  3. 173. Binary Search Tree Iterator -- 迭代器

    Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the ro ...

  4. mysqli使用记录

    1.批量插入数据insert 方式一:insert into tableName (id,name,age) values ('1','张三','12'),('2','李四','16'),('3',' ...

  5. Spring的注解@Qualifier(二十五)

    转载:https://www.cnblogs.com/smileLuckBoy/p/5801678.html 近期在捯饬spring的注解,现将遇到的问题记录下来,以供遇到同样问题的童鞋解决~ 先说明 ...

  6. POJ 3984 迷宫问题 bfs 难度:0

    http://poj.org/problem?id=3984 典型的迷宫问题,记录最快到达某个点的是哪个点即可 #include <cstdio> #include <cstring ...

  7. css单位长度

    CSS长度单位 单位 含义 em 相对于父元素的字体大小 ex 相对于小写字母”x”的高度 gd 一般用在东亚字体排版上,这个与英文并无关系 rem 相对于根元素字体大小 vw 相对于视窗的宽度:视窗 ...

  8. c/c++ socket函数详解

    c/c++ socket函数详解 注意: 使用socketAPI前,要先将相关链接库(Ws2_32.lib)加入链接,并使用WSAStartUp函数初始化.每个socket函数都可能失败(返回-1), ...

  9. vue music 歌单组件

    在data里面定义 discList: [] methods: { _getRecommend() { getRecommend().then((res) => { if(res.code == ...

  10. Codeforces 148B: Escape

    题目链接:http://codeforces.com/problemset/problem/148/B 题意:公主从龙的洞穴中逃跑,公主的速度为vp,龙的速度为vd,在公主逃跑时间t时,龙发现公主逃跑 ...