C++ Easy Solutions🔥 || BFS 🔥|| DFS🔥 || Fastest🔥🔥
\n# Approach : DFS\n Describe your approach to solving the problem. \n# Code 1\n\n/**\n * Definition for a binary tree node.\n * struct TreeNode {\n *     int v
\n# Approach : DFS\n<!-- Describe your approach to solving the problem. -->\n# Code 1\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 dfs[10000] = {0};\n    int max_traverse = -1;\n  \n    void findDepthSum(TreeNode* root, int d) {\n        if (root==NULL)\n            return;\n        dfs[d] += root->val;\n        findDepthSum(root->left,d+1);\n        findDepthSum(root->right,d+1);\n        \n        if (d > max_traverse)\n            max_traverse = d;\n    }\n    \n    \n    int maxLevelSum(TreeNode* root) {\n\n\tfindDepthSum(root,1);\n\tint maxSum = INT_MIN;\n\tint levelIdx = -1;\n\tfor(int i=1;i<=max_traverse;++i) {\n\t\tcout << dfs[i] << " ";\n\t\tif (maxSum < dfs[i]) {\n\t\t\tmaxSum = dfs[i];\n\t\t\tlevelIdx = i;\n\t\t}\n\t}\n\treturn levelIdx;\n}\n};\n```\n\n# Approach : BFS\n<!-- Describe your approach to solving the problem. -->\n# Code 2\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 maxLevelSum(TreeNode* root) {\n        int maxLevel = 0;\n        int maxLevelS = INT_MIN;\n        \n        queue<TreeNode*> q;\n        queue<TreeNode*> nextLevel;\n        \n        q.push(root);\n        int level = 1;\n        \n        while (!q.empty()) {\n            \n            int sum = 0;\n            int size = q.size();\n            for (int i = 0; !q.empty() && i < size; i++) {\n                auto* curr = q.front();\n                sum += curr->val;\n                q.pop();\n                if (curr->left)\n                    nextLevel.push(curr->left);\n            \n                if (curr->right)\n                    nextLevel.push(curr->right);\n            }\n            \n            if (q.empty()) {\n                swap(q, nextLevel);\n                if (sum > maxLevelS) {\n                    maxLevelS= sum;\n                    maxLevel = level;\n                }\n            }\n            \n            level++;\n        }\n        \n        return maxLevel;\n    }\n};\n```\n# Approach : Map ( To store sum)\n<!-- Describe your approach to solving the problem. -->\n# Code 3\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 maxLevelSum(TreeNode* root) {\n        queue<TreeNode*> Q;\n        if (root == nullptr) return 0;\n        Q.push(root);\n        int level = 0;\n        map<int, int> sum;\n        while (!Q.empty()) {\n            int cursum = 0;\n            int count = Q.size();\n            level ++;\n            // expand nodes of same level\n            for (int i = 0; i < count; ++ i) {\n                auto p = Q.front();\n                cursum += p->val;\n                if (p->left != nullptr) Q.push(p->left);\n                if (p->right != nullptr) Q.push(p->right);\n                Q.pop();\n            }\n            if (sum.find(cursum) == sum.end()) {\n                sum[cursum] = level;\n            } else {\n                sum[cursum] = min(sum[cursum], level);\n            }\n        }\n        return rbegin(sum)->second;\n    }\n};\n```
['C++']