LeetCode
来源:互联网 发布:js字符串去掉换行符 编辑:程序博客网 时间:2024/06/18 08:30
102. 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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7]]
层次遍历,用两个队列实现的。时间复杂度O(n),空间复杂度O(n)
评论区有用一个队列实现的,是在每层后面加一个NULL,只要扫到NULL就说明换层,嗯,靠谱
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int> > ans; if (!root) return ans; queue<TreeNode*> qu; queue<TreeNode*> last; qu.push(root); TreeNode* pre = root; vector<int> miao; while (true) { bool flag = false;; while (!qu.empty()) { flag = true; TreeNode* cur = qu.front(); miao.push_back(cur->val); last.push(cur); qu.pop(); } if (flag) { ans.push_back(miao); miao.clear(); } while (!last.empty()) { flag = true; TreeNode* cur = last.front(); if (cur->left) qu.push(cur->left); if (cur->right) qu.push(cur->right); last.pop(); } if (!flag) break; } return ans; }};
class Solution {public: vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > result; if (!root) return result; queue<TreeNode*> q; q.push(root); q.push(NULL); vector<int> cur_vec; while(!q.empty()) { TreeNode* t = q.front(); q.pop(); if (t==NULL) { result.push_back(cur_vec); cur_vec.resize(0); if (q.size() > 0) { q.push(NULL); } } else { cur_vec.push_back(t->val); if (t->left) q.push(t->left); if (t->right) q.push(t->right); } } return result; }};
107. Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).
For example:
Given binary tree [3,9,20,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3]]
层次遍历倒着输出,跟上边那题一样。这次借鉴了上一题评论区的做法,采用一个NULL指针放入每一层的末尾,只需一个队列即可。时间复杂度O(n),空间复杂度O(n)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int> >ans; if (!root) return ans; queue<TreeNode*> q; q.push(root); q.push(NULL); vector<int> miao; while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); if (cur == NULL) { ans.insert(ans.begin(), miao); miao.clear(); if (!q.empty()) q.push(NULL); } else { miao.push_back(cur->val); if (cur->left) q.push(cur->left); if (cur->right) q.push(cur->right); } } return ans; }};
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 array.
Example 1:
Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].
Note:
- The range of node's value is in the range of 32-bit signed integer.
额,跟上边两道没啥不一样的,就是求个平均数。时间复杂度O(n),空间复杂度O(n)
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: vector<double> averageOfLevels(TreeNode* root) { vector<double> ans; if (!root) return ans; queue<TreeNode*> q; q.push(root); q.push(NULL); double sum = 0.0; int cnt = 0; while (!q.empty()) { TreeNode* cur = q.front(); q.pop(); if (cur == NULL) { ans.push_back(sum * 1.0 / cnt); sum = 0; cnt = 0; if (!q.empty()) q.push(NULL); } else { sum += cur->val; cnt++; if (cur->left) q.push(cur->left); if (cur->right) q.push(cur->right); } } return ans; }};
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- java将控制台输入的字符串按照空格划分数据存入数组
- RuntimeError: maximum recursion depth exceeded while calling a Python object解决办法
- join()方法
- Javascript里有个C:Part 3
- Android往WebView中填写信息
- LeetCode
- 复杂链表的复制
- 廖雪峰Python教程学习笔记4-函数式编程
- 程序员内部培训与个人发展杂谈
- JPA实体类中的注解
- Shell系列—Shell 传递参数
- String 、InputStream、Reader 之间的转换
- 三家人 CSU
- smplayer centos7