From c4428463243dc9f2bbd48acb59f966ad65706386 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Tue, 31 Jan 2023 12:28:03 +0800 Subject: [PATCH] s0513 --- include/s0513_find_bottom_left_tree_value.hpp | 15 ++++++++++++++ src/s0513_find_bottom_left_tree_value.cpp | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 include/s0513_find_bottom_left_tree_value.hpp create mode 100644 src/s0513_find_bottom_left_tree_value.cpp diff --git a/include/s0513_find_bottom_left_tree_value.hpp b/include/s0513_find_bottom_left_tree_value.hpp new file mode 100644 index 0000000..0b8fa77 --- /dev/null +++ b/include/s0513_find_bottom_left_tree_value.hpp @@ -0,0 +1,15 @@ +#ifndef S0513_FIND_BOTTOM_LEFT_TREE_VALUE_HPP +#define S0513_FIND_BOTTOM_LEFT_TREE_VALUE_HPP + +#include + +#include "structures.hpp" + +using namespace std; + +class S0513 { + public: + int findBottomLeftValue(TreeNode* root); +}; + +#endif diff --git a/src/s0513_find_bottom_left_tree_value.cpp b/src/s0513_find_bottom_left_tree_value.cpp new file mode 100644 index 0000000..9657be2 --- /dev/null +++ b/src/s0513_find_bottom_left_tree_value.cpp @@ -0,0 +1,20 @@ +#include "s0513_find_bottom_left_tree_value.hpp" + +int findBottomLeftValue(TreeNode* root) { + int result{0}; + queue 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; +}