[leetcode 515]Find Largest Value in Each Tree Row
2017-03-03 13:37
357 查看
问题描述
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]
代码
深搜而已。int cal_depth(TreeNode* root) { if (root == NULL) return 0; return max(cal_depth(root->left),cal_depth(root->right))+1; } void cal(TreeNode* root,vector<int>& result,int depth) { if (root == NULL) return; if (result[depth] < root->val) result[depth] = root->val; cal(root->left,result,depth+1); cal(root->right,result,depth+1); } vector<int> largestValues(TreeNode* root) { int depth = cal_depth(root); vector<int> result(depth,1<<31); cal(root,result,0); return result; }
相关文章推荐
- LeetCode 515 Find Largest Value in Each Tree Row(二叉树层序遍历)
- 【LeetCode515 Find Largest Value in Each Tree Row】二叉树每行的最大值
- Leetcode 515 Find Largest Value in Each Tree Row
- leetcode 515 Find Largest Value in Each Tree Row C++
- [LeetCode]515 Find Largest Value in Each Tree Row(dfs)
- leetcode515 Find Largest Value In Each Tree Row Java
- [LeetCode] Find Largest Value in Each Tree Row 找树每行最大的结点值
- LeetCode:Find Largest Value in Each Tree Row
- 515[Medium]:Find Largest Value in Each Tree Row
- LeetCode Find Largest Value in Each Tree Row
- LeetCode之Find Largest Value in Each Tree Row
- 【LEETCODE】 Find Largest Value in Each Tree Row javascript实现
- LeetCode算法问题17 —— Find Largest Value in Each Tree Row
- LeetCode——Find Largest Value in Each Tree Row
- 【LeetCode】Find Largest Value in Each Tree Row 解题报告
- Week Training: 515 Find Largest Value in Each Tree Row
- LeetCode--Find Largest Value in Each Tree Row
- LeetCode: Find Largest Value in Each Tree Row
- 算法分析与设计丨第三周丨LeetCode(6)——Find Largest Value in Each Tree Row(Medium)
- LeetCode之Find Largest Value in Each Tree Row(Kotlin)