This commit is contained in:
parent
63f60259ee
commit
c442846324
15
include/s0513_find_bottom_left_tree_value.hpp
Normal file
15
include/s0513_find_bottom_left_tree_value.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef S0513_FIND_BOTTOM_LEFT_TREE_VALUE_HPP
|
||||||
|
#define S0513_FIND_BOTTOM_LEFT_TREE_VALUE_HPP
|
||||||
|
|
||||||
|
#include <queue>
|
||||||
|
|
||||||
|
#include "structures.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class S0513 {
|
||||||
|
public:
|
||||||
|
int findBottomLeftValue(TreeNode* root);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
20
src/s0513_find_bottom_left_tree_value.cpp
Normal file
20
src/s0513_find_bottom_left_tree_value.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#include "s0513_find_bottom_left_tree_value.hpp"
|
||||||
|
|
||||||
|
int findBottomLeftValue(TreeNode* root) {
|
||||||
|
int result{0};
|
||||||
|
queue<TreeNode *> q;
|
||||||
|
if (root) q.push(root);
|
||||||
|
int size{0};
|
||||||
|
TreeNode *front = root;
|
||||||
|
while (!q.empty()) {
|
||||||
|
size = q.size();
|
||||||
|
for (int i{0}; i < size; ++i) {
|
||||||
|
front = q.front();
|
||||||
|
q.pop();
|
||||||
|
if (i == 0) result = front->val;
|
||||||
|
if (front->left) q.push(front->left);
|
||||||
|
if (front->right) q.push(front->right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user