[LeetCode]515. Find Largest Value in Each Tree Row
来源:互联网 发布:淘宝女装美工 编辑:程序博客网 时间:2024/06/15 19:00
[LeetCode]515. Find Largest Value in Each Tree Row
题目描述
思路
递归,深度优先
使用数组存储对应层的最大值
代码
#include <iostream>#include <vector>#include <algorithm>using namespace std;struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: vector<int> largestValues(TreeNode* root) { vector<int> res; if (root == NULL) return res; res.push_back(root->val); travel(root, 0, res); return res; }private: void travel(TreeNode* root, int level, vector<int>& res) { if (root == NULL) return; if (level < res.size()) res[level] = max(res[level], root->val); else res.push_back(root->val); travel(root->left, level + 1, res); travel(root->right, level + 1, res); }};int main() { TreeNode* t1 = new TreeNode(1); TreeNode* t2 = new TreeNode(3); TreeNode* t3 = new TreeNode(2); TreeNode* t4 = new TreeNode(5); TreeNode* t5 = new TreeNode(3); TreeNode* t6 = new TreeNode(9); t1->left = t2; t1->right = t3; t2->left = t4; t2->right = t5; t3->right = t6; Solution s; vector<int> res = s.largestValues(t1); for (int num : res) cout << num << " "; cout << endl; system("pause"); return 0;}
阅读全文
0 0
- LeetCode 515. Find Largest Value in Each Tree Row
- LeetCode OJ 515. 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
- 【LeetCode】515. 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
- leetcode 515. 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
- leetcode 515. 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
- LeetCode#515. 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
- [LeetCode] 515. Find Largest Value in Each Tree Row
- 哲学家就餐问题
- matlab绘图时legend显示分式(latex风格)
- ob_flush(); flush(); 1-100显示
- Stream 过滤
- 51nod 1416 两点
- [LeetCode]515. Find Largest Value in Each Tree Row
- nodejs中cookie,session保存内存,缓存,数据库中
- 数据库中查询的数据是多条,可是显示出来的只有一条,为什么?
- 我所理解的Python里method和function的区别(关键词:method/方法/function/函数)
- Android--仿爱奇艺 首页轮播图
- Sum HDU 4704
- 7. Reverse Integer
- 内部10位ADC
- Linux内存管理slab分配器