File tree Expand file tree Collapse file tree 2 files changed +88
-0
lines changed
lcof2/剑指 Offer II 044. 二叉树每层的最大值 Expand file tree Collapse file tree 2 files changed +88
-0
lines changed Original file line number Diff line number Diff line change 93
93
94
94
```
95
95
96
+ ### **C++**
97
+
98
+ ```cpp
99
+ class Solution {
100
+ public:
101
+ vector<int> largestValues(TreeNode* root) {
102
+ vector<int> res;
103
+ if( !root )
104
+ {
105
+ return res;
106
+ }
107
+
108
+ deque<TreeNode* > deq;
109
+ deq.push_back(root);
110
+ while( !deq.empty())
111
+ {
112
+ int size = deq.size();
113
+ int maxnum = INT_MIN;
114
+ for (int i = 0; i < size; i++)
115
+ {
116
+ TreeNode* ptr = deq.front();
117
+ deq.pop_front();
118
+ if(maxnum < ptr->val)
119
+ {
120
+ maxnum = ptr->val;
121
+ }
122
+
123
+ if(ptr->left)
124
+ {
125
+ deq.push_back(ptr->left);
126
+ }
127
+
128
+ if(ptr->right)
129
+ {
130
+ deq.push_back(ptr->right);
131
+ }
132
+ }
133
+
134
+ res.push_back(maxnum);
135
+ }
136
+
137
+ return res;
138
+ }
139
+ };
140
+ ```
141
+
96
142
### **...**
97
143
98
144
```
99
145
100
146
```
101
147
102
148
<!-- tabs:end -->
149
+
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ vector<int> largestValues(TreeNode* root) {
4
+ vector<int> res;
5
+ if( !root )
6
+ {
7
+ return res;
8
+ }
9
+
10
+ deque<TreeNode* > deq;
11
+ deq.push_back(root);
12
+ while( !deq.empty())
13
+ {
14
+ int size = deq.size();
15
+ int maxnum = INT_MIN;
16
+ for (int i = 0; i < size; i++)
17
+ {
18
+ TreeNode* ptr = deq.front();
19
+ deq.pop_front();
20
+ if(maxnum < ptr->val)
21
+ {
22
+ maxnum = ptr->val;
23
+ }
24
+
25
+ if(ptr->left)
26
+ {
27
+ deq.push_back(ptr->left);
28
+ }
29
+
30
+ if(ptr->right)
31
+ {
32
+ deq.push_back(ptr->right);
33
+ }
34
+ }
35
+
36
+ res.push_back(maxnum);
37
+ }
38
+
39
+ return res;
40
+ }
41
+ };
You can’t perform that action at this time.
0 commit comments