题解:Find Largest Value in Each Tree Rowt
来源:互联网 发布:腾讯视频mac 编辑:程序博客网 时间:2024/05/18 00:53
题目: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]不断遍历并且更新数组即可,使用的是递归的
#include <iostream>#include <vector>using namespace std;struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};void search(TreeNode* root, vector<int>& result, int d){ if(result.size() < d + 1) result.push_back(root->val); else { if (result[d] < root->val) result[d] = root->val; search(root->left, result, d + 1); search(root->right, result, d + 1);}}vector<int> largestValues(TreeNode* root) { vector<int> result; result.push_back(root->val); search(root->left, result, 1); search(root->right, result, 1); return result;}
0 0
- 题解:Find Largest Value in Each Tree Rowt
- LeetCode515. Find Largest Value in Each Tree Row题解
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- Find Largest Value in Each Tree Row
- LeetCode题解(Week5): 515. Find Largest Value in Each Tree Row
- Tree——Find Largest Value in Each Tree Row
- LeetCode 515. Find Largest Value in Each Tree Row
- 515. Find Largest Value in Each Tree Row
- leetcode515: Find Largest Value in Each Tree Row
- LeetCode OJ 515. Find Largest Value in Each Tree Row
- leetcode515 Find Largest Value In Each Tree Row Java
- Java.lang.Class
- PHP Notice: undefined index 完美解决方法
- 高等数学-《函数与极限》总结笔记
- 我的向往
- Awesome
- 题解:Find Largest Value in Each Tree Rowt
- Poj3259(floyd)
- jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中
- 高等数学-对函数极限的理解
- 《设计模式》-- 策略模式
- Android错误笔记---com.google.code.findbugs:jsr305
- HttpServletRequest和HttpServletResponse详解
- SuperSwipeRefreshLayout(非浸入式下拉刷新、上拉加载)
- 最通俗易懂的背包问题