Skip to content

Commit 6daa496

Browse files
committed
Time: 12 ms (76.12%), Space: 22.4 MB (53.35%) - LeetHub
1 parent 3f9048b commit 6daa496

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
map<int,int> mp;
14+
public:
15+
void dfs(TreeNode* root, int ht){
16+
if(root == NULL)
17+
return;
18+
// current
19+
if(mp.find(ht) == mp.end())
20+
mp[ht] = root->val;
21+
else
22+
mp[ht] = max(mp[ht], root->val);
23+
dfs(root->left, ht + 1);
24+
dfs(root->right, ht + 1);
25+
}
26+
27+
vector<int> largestValues(TreeNode* root) {
28+
dfs(root, 0);
29+
vector<int> ans;
30+
31+
for(auto x: mp){
32+
ans.push_back(x.second);
33+
}
34+
35+
return ans;
36+
}
37+
};

0 commit comments

Comments
 (0)