Skip to content

Commit a343ffb

Browse files
committed
0515 solved.
1 parent b3397d0 commit a343ffb

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.22)
2+
project(cpp_0515)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0515 main.cpp)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/// Source : https://leetcode.com/problems/find-largest-value-in-each-tree-row/
2+
/// Author : liuyubobobo
3+
/// Time : 2022-06-23
4+
5+
#include <iostream>
6+
#include <vector>
7+
8+
using namespace std;
9+
10+
11+
/// DFS
12+
/// Time Complexity: O(n)
13+
/// Space Complexity: O(depth)
14+
15+
/// Definition for a binary tree node.
16+
struct TreeNode {
17+
int val;
18+
TreeNode *left;
19+
TreeNode *right;
20+
TreeNode() : val(0), left(nullptr), right(nullptr) {}
21+
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
22+
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
23+
};
24+
25+
class Solution {
26+
public:
27+
vector<int> largestValues(TreeNode* root) {
28+
29+
vector<int> res;
30+
dfs(root, 0, res);
31+
return res;
32+
}
33+
34+
private:
35+
void dfs(TreeNode* node, int depth, vector<int>& res){
36+
37+
if(!node) return;
38+
39+
if(depth == res.size()) res.push_back(node->val);
40+
else res[depth] = max(res[depth], node->val);
41+
42+
dfs(node->left, depth + 1, res);
43+
dfs(node->right, depth + 1, res);
44+
}
45+
};
46+
47+
48+
int main() {
49+
50+
return 0;
51+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
544544
| | | | | | |
545545
| 513 | [Find Bottom Left Tree Value](https://leetcode.com/problems/find-bottom-left-tree-value/) | [] | [C++](0501-1000/0513-Find-Bottom-Left-Tree-Value/cpp-0513/) | | |
546546
| | | | | | |
547+
| 515 | [Find Largest Value in Each Tree Row](https://leetcode.com/problems/find-largest-value-in-each-tree-row/) | [] | [C++](0501-1000/0515-Find-Largest-Value-in-Each-Tree-Row/cpp-0515/) | | |
547548
| 516 | [Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/) | [] | [C++](0501-1000/0516-Longest-Palindromic-Subsequence/cpp-0516/) | | |
548549
| 517 | [Super Washing Machines](https://leetcode.com/problems/super-washing-machines/) | [solution](https://leetcode.com/problems/super-washing-machines/solution/) | [C++](0501-1000/0517-Super-Washing-Machines/cpp-0517/) | | |
549550
| 518 | [Coin Change 2](https://leetcode.com/problems/coin-change-2/description/) | [] | [C++](0501-1000/0518-Coin-Change-2/cpp-0518/) | | |

0 commit comments

Comments
 (0)