[LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
You need to find the largest value in each row of a binary tree.
Example:
Input: 1/ \3 2/ \ \ 5 3 9 Output: [1, 3, 9]
这道题让我们找二叉树每行的最大的结点值,那么实际上最直接的方法就是用层序遍历,然后在每一层中找到最大值,加入结果res中即可,参见代码如下:
解法一:
class Solution { public:vector<int> largestValues(TreeNode* root) {if (!root) return {};vector<int> res;queue<TreeNode*> q;q.push(root);while (!q.empty()) {int n = q.size(), mx = INT_MIN;for (int i = 0; i < n; ++i) {TreeNode *t = q.front(); q.pop();mx = max(mx, t->val);if (t->left) q.push(t->left);if (t->right) q.push(t->right);}res.push_back(mx);}return res;} };
如果我们想用迭代的方法来解,可以用先序遍历,这样的话就需要维护一个深度变量depth,来记录当前结点的深度,如果当前深度大于结果res的长度,说明这个新一层,我们将当前结点值加入结果res中,如果不大于res的长度的话,我们用当前结点值和结果res中对应深度的那个结点值相比较,取较大值赋给结果res中的对应深度位置,参见代码如下:
解法二:
class Solution { public:vector<int> largestValues(TreeNode* root) {if (!root) return {};vector<int> res;helper(root, 1, res);return res;}void helper(TreeNode* root, int depth, vector<int>& res) {if (depth > res.size()) res.push_back(root->val);else res[depth - 1] = max(res[depth - 1], root->val);if (root->left) helper(root->left, depth + 1, res);if (root->right) helper(root->right, depth + 1, res);} };
参考资料:
https://discuss.leetcode.com/topic/79241/simple-and-easy-understand-c-dfs-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值相关推荐
- LeetCode 515. Find Largest Value in Each Tree Row
515. Find Largest Value in Each Tree Row You need to find the largest value in each row of a binary ...
- leetcode -- 515. Find Largest Value in Each Tree Row
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ ...
- [leetcode-515-Find Largest Value in Each Tree Row]
You need to find the largest value in each row of a binary tree. Example: Input: 1 / \ 3 2 ...
- 【Breadth-first Search 】515. Find Largest Value in Each Tree Row
输入:一颗二叉树 输出:这棵树每一层的最大值. 分析:和513 题目一样,处理层次问题,使用BFS最直观.使用和513一样的模板,只是记录下该层最大值即可. 分析2:用DFS处理层次遍历的问题,需要把 ...
- Java描述 LeetCode,513. Find Bottom Left Tree Value 找左下角的值
大家好,我是河海哥,专注于后端,如果可以的话,想做一名code designer而不是普通的coder,一起见证河海哥的成长,您的评论与赞是我的最大动力,如有错误还请不吝赐教,万分感谢.一起支持原创吧 ...
- LeetCode——Kth Largest Element in an Array
LeetCode--Kth Largest Element in an Array Question Find the kth largest element in an unsorted array ...
- LeetCode:Largest Number - 求整型数组中各元素可拼合成的最大数字
2019独角兽企业重金招聘Python工程师标准>>> 1.题目名称 Largest Number(求整型数组中各元素可拼合成的最大数字) 2.题目地址 https://leetco ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- LeetCode 84. Largest Rectangle in Histogram
LeetCode 84. Largest Rectangle in Histogram Solution1:我的答案 循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!! ...
最新文章
- ubuntu ftp服务器搭建,绝对有效,操作简单
- gradle 构建包含源码配置
- JDK8新特性之Lambda表达式
- getexternalfilesdir 相册_音乐相册(电子相册制作)V5.2 安卓最新版
- VMware卸载有残留,再安装时报错提示MSI Failed
- shell 需要注意的点
- SinglepassTextCluster项目:基于single-pass算法思想的自动文本聚类组件
- Windows环境中在同一个Tomcat下发布不同端口号的不同web程序
- csdn学院 python_确认!别再相信Python了! 程序员:就你敢说...
- Qt创建多线程的两种方法
- 7-4 输出三角形字符阵列 (15 分)
- 苹果mac幻灯片演示文稿制作软件:PowerPoint 2019
- sqlplus / as sysdba ORA-01017
- Python爬虫——多线程爬取阳光问政
- Fresco之强大之余的痛楚
- PS抠头发妙法(原创技巧)
- Python算法--查找兄弟单词
- SAAS-HRM-day1
- Matlab GUI中的hObjecthandles
- 海思3556V200 PQTools 工具环境搭建