Skip to content

Commit 92f9405

Browse files
daliang111willrlzhangmaolonglong
authored
feat: add cpp solution to lcof2 problem: NO.045 (doocs#566)
* feat: add cpp solution to lcof2 problem: NO.045 * feat: add cpp solution to lcof2 problem: NO.045 use queue * format code Co-authored-by: willrlzhang <willrlzhang@tencent.com> Co-authored-by: MaoLongLong <382084620@qq.com>
1 parent 9f696cd commit 92f9405

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

lcof2/剑指 Offer II 045. 二叉树最底层最左边的值/README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,49 @@
6464

6565
```
6666

67+
### **C++**
68+
69+
```cpp
70+
class Solution {
71+
public:
72+
int findBottomLeftValue(TreeNode* root) {
73+
if (!root)
74+
{
75+
return 0;
76+
}
77+
78+
int res = root->val;
79+
queue<TreeNode*> que;
80+
que.push(root);
81+
while (!que.empty())
82+
{
83+
int size = que.size();
84+
for (int i = 0; i < size; i++)
85+
{
86+
TreeNode* ptr = que.front();
87+
que.pop();
88+
if (i == 0)
89+
{
90+
res = ptr->val;
91+
}
92+
93+
if (ptr->left)
94+
{
95+
que.push(ptr->left);
96+
}
97+
98+
if (ptr->right)
99+
{
100+
que.push(ptr->right);
101+
}
102+
}
103+
}
104+
105+
return res;
106+
}
107+
};
108+
```
109+
67110
### **...**
68111

69112
```
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
int findBottomLeftValue(TreeNode* root) {
4+
if (!root)
5+
{
6+
return 0;
7+
}
8+
9+
int res = root->val;
10+
queue<TreeNode*> que;
11+
que.push(root);
12+
while (!que.empty())
13+
{
14+
int size = que.size();
15+
for (int i = 0; i < size; i++)
16+
{
17+
TreeNode* ptr = que.front();
18+
que.pop();
19+
if (i == 0)
20+
{
21+
res = ptr->val;
22+
}
23+
24+
if (ptr->left)
25+
{
26+
que.push(ptr->left);
27+
}
28+
29+
if (ptr->right)
30+
{
31+
que.push(ptr->right);
32+
}
33+
}
34+
}
35+
36+
return res;
37+
}
38+
};

0 commit comments

Comments
 (0)