-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path515.FindLargestValueinEachTreeRow.cpp
52 lines (46 loc) · 1.16 KB
/
515.FindLargestValueinEachTreeRow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//do by my self ,利用dfs,分别记录每层时的值。x
class Solution {
public:
vector<int> largestValues(TreeNode* root) {
vector<int> res;
dfs(root,0);
for(const auto& item:cnt){
res.push_back(item.second);
}
return res;
}
void dfs(TreeNode* root, int level){
if(!root)
return;
if(root){
if(cnt.find(level)== cnt.end()){
cnt[level] = root->val;
}
else{
if(cnt[level]<root->val)
cnt[level] = root->val;
}
}
dfs(root->left,level+1);
dfs(root->right,level+1);
}
private:
map<int,int> cnt;
};
//pthon
def findValueMostElement(self, root):
maxes = []
row = [root]
while any(row):
maxes.append(max(node.val for node in row))
row = [kid for node in row for kid in (node.left, node.right) if kid]//brilliant
return maxes