-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem_STOII_0045_findBottomLeftValue.cc
72 lines (66 loc) · 1.4 KB
/
Problem_STOII_0045_findBottomLeftValue.cc
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <functional>
#include <iostream>
#include <queue>
using namespace std;
struct TreeNode
{
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution
{
public:
// bfs
int findBottomLeftValue1(TreeNode *root)
{
queue<TreeNode *> q;
q.push(root);
int ans = root->val;
while (!q.empty())
{
TreeNode *cur = q.front();
q.pop();
// 先让右孩子进队,后让左节点进队
// 这样每层最后访问的节点是最左的子节点
if (cur->right)
{
q.push(cur->right);
}
if (cur->left)
{
q.push(cur->left);
}
ans = cur->val;
}
return ans;
}
// dfs
int findBottomLeftValue2(TreeNode *root)
{
int curValue;
int curHeight;
std::function<void(TreeNode *, int)> dfs = [&](TreeNode *cur, int height)
{
if (cur == nullptr)
{
return;
}
height++;
// 递归先遍历左子树
dfs(cur->left, height);
dfs(cur->right, height);
// 找到深度最深的左子节点
if (height > curHeight)
{
curHeight = height;
curValue = cur->val;
}
};
dfs(root, 0);
return curValue;
}
};