爨爨爨好

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

▶ 第 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 + 1);
11         table[level].push_back(root->val);
12         visit(root->right, table, level + 1);
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, 0);
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 + 1);
11         table[level].push_back(root->val);
12         visit(root->right, table, level + 1);
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, 0);
21         for (int i = 1; i < output.size(); i += 2)
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 + 1);
11         table[level].push_back(root->val);
12         visit(root->right, table, level + 1);
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, 0);
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 + 1);
11         table[level].push_back(root->val);
12         visit(root->right, table, level + 1);
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, 0);
24         for (i = 0; i < table.size(); i++)
25         {
26             for (j = 0, 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 };

 

posted on 2018-02-03 23:19  爨爨爨好  阅读(165)  评论(0编辑  收藏  举报