BFS vs DFS||3ms Beats 98.29%
Intuition\n Describe your first thoughts on how to solve this problem. \nBFS uses queue  which is FIFO\nDFS is a recursion which uses system stack & FILO\n# App
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nBFS uses queue  which is FIFO\nDFS is a recursion which uses system stack & FILO\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBe careful for these 2 different containers. The feeding order can not be wrong.\n\nBFS has a simple version for which there is no to feed info `(node, level)`. A simple version for BFS is also presented.\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$ \n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(height(root))$$ \n# Code using BFS||3ms Beats 98.29%\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    using info=pair<TreeNode*, int>;\n    int findBottomLeftValue(TreeNode* root) {\n        int ans=0, level=-1;\n        queue<info> q;\n        q.emplace(root, 0);\n        while(!q.empty()){\n            auto [node, h]=q.front();\n            q.pop();\n            if (!node) continue;\n            if (h>=level){\n                ans=node->val;\n                level=h;\n            }\n            q.emplace(node->right, h+1);//FIFO\n            q.emplace(node->left, h+1);\n        }\n\n        return ans;\n    }\n};\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# C++ using DFS||4ms Beats 90.18%\n```\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNode() : val(0), left(nullptr), right(nullptr) {}\n *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}\n *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}\n * };\n */\nclass Solution {\npublic:\n    int ans=0, level=-1;\n    void dfs(TreeNode* node, int h){\n        if (!node) return;\n\n        if (h>level){\n            ans=node->val;\n            level=h;\n        } \n        dfs(node->left, h+1);// Recursion uses system stack FILO\n        dfs(node->right, h+1);\n    }\n    int findBottomLeftValue(TreeNode* root) {\n        dfs(root, 0);\n        return ans;\n    }\n};\n\n\nauto init = []() {\n    ios::sync_with_stdio(0);\n    cin.tie(0);\n    cout.tie(0);\n    return \'c\';\n}();\n```\n# C++ simple BFS\n```\nclass Solution {\npublic:\n//    using info=pair<TreeNode*, int>;\n    int findBottomLeftValue(TreeNode* root) {\n        int ans=0, level=-1;\n        queue<TreeNode*> q;//no need info for level, BFS is good \n        q.emplace(root);\n        while(!q.empty()){\n            auto node=q.front();\n            q.pop();\n            if (!node) continue;\n        //    if (h>=level){//always true drop it, & make it simpler\n            ans=node->val;\n        //        level=h;\n       //     }\n            q.emplace(node->right);//FIFO\n            q.emplace(node->left);\n        }\n\n        return ans;\n    }\n};\n```
['Depth-First Search', 'Breadth-First Search', 'C++']