-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path513_Find_Bottom_Left_Tree_Value.cpp
82 lines (78 loc) · 1.84 KB
/
513_Find_Bottom_Left_Tree_Value.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include<climits>
#include<cmath>
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode():left(NULL), right(NULL){}
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
int findBottomLeftValue(TreeNode* root)
{
queue<TreeNode*> q;
vector<int> record;
q.push(root);
int cnt = 0;
int cur_level = 0;
int null_cnt = 0;
int level_cnt = 0;
int max = -1;
while(not q.empty())
{
TreeNode* x = q.front();
if(x != NULL )
{
q.push(x->left);
q.push(x->right);
record.push_back(x->val);
}
else
{
record.push_back(INT_MAX);
q.push(NULL);
q.push(NULL);
++null_cnt;
}
q.pop();
cnt++;
level_cnt++;
if(null_cnt == pow(2, cur_level))
break;
if(level_cnt == pow(2, cur_level))
{
++cur_level;
null_cnt = 0;
level_cnt = 0;
}
}
for(int i = 0; i < pow(2, i); i++)
{
int index = pow(2, cur_level-1) -1 + i;
if(record[index] != INT_MAX)
return record[index];
}
return -1;
}
};
int main(int argc, const char* argv[])
{
TreeNode root(2);
TreeNode a(5);
TreeNode b(3);
TreeNode c(9);
TreeNode d(1);
TreeNode e(3);
root.left = &d;
root.right = &e;
// d.left = &a;
// d.right = &b;
// e.right = &c;
Solution s;
cout<<s.findBottomLeftValue(&root);
}