DFS and BFS | Easy C++ Solutions | Beats 98.29% | Trees
\n# DFS\n\n\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int val;\n *     TreeNode *left;\n *     TreeNode *right;\n *     TreeNo
\n# DFS\n\n\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    void dfs(TreeNode* node, int height, int& maxHeight, int& result) {\n        if(!node) return;\n        if(height>maxHeight){\n            maxHeight=height;\n            result=node->val;\n        }\n        dfs(node->left,height+1,maxHeight,result);\n        dfs(node->right,height+1,maxHeight,result);\n    }\n\n    int findBottomLeftValue(TreeNode* root) {\n        int maxHeight=0, result=root->val;\n        dfs(root,0,maxHeight,result);\n    return result;\n    }\n};\n```\n\n---------------------\n\n# BFS \n\n```\nclass Solution {\npublic:\n    int findBottomLeftValue(TreeNode* root) {\n        int result;\n        queue<TreeNode*> q;\n        q.push(root);\n        while(!q.empty()){\n            int n=q.size();\n            for(int i=0;i<n;i++){\n                TreeNode *curr=q.front();\n                q.pop(); \n                if(i==0) result=curr->val;\n                if(curr->left) q.push(curr->left);\n                if(curr->right) q.push(curr->right);\n            }\n        }\n    return result;\n    }\n};\n\n```\n\n-------------\n\n### If you find this solution helpful, consider giving it an upvote!\n\n#### Do check out all other solutions posted by me here: https://github.com/adityajamwal02/Leetcode-Community-Solutions\n\n-------\n\n
['Tree', 'Binary Tree', 'C++']