File tree Expand file tree Collapse file tree 2 files changed +81
-0
lines changed
lcof2/剑指 Offer II 045. 二叉树最底层最左边的值 Expand file tree Collapse file tree 2 files changed +81
-0
lines changed Original file line number Diff line number Diff line change 64
64
65
65
```
66
66
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
+
67
110
### ** ...**
68
111
69
112
```
Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments