leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

commit 2cf6f4cf29eb000619b440134066739353494bf4
parent a4090374dcaad3d0c43602f47dadd0399d6a6148
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date:   Thu, 15 Jun 2023 19:43:50 +0200

Daily Problem

Diffstat:
AProblems/1161.cpp | 24++++++++++++++++++++++++
MREADME.md | 1+
2 files changed, 25 insertions(+), 0 deletions(-)

diff --git a/Problems/1161.cpp b/Problems/1161.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int maxLevelSum(TreeNode *root) { + queue<TreeNode *> q; + q.push(root); + + int maxi = INT_MIN, index = -1; + for (int lvl = 1; !q.empty(); lvl++) { + int sum = 0; + for (int k = q.size(); k > 0; k--) { + TreeNode *root = q.front(); + q.pop(); + sum += root->val; + if (root->left) q.push(root->left); + if (root->right) q.push(root->right); + } + if (sum > maxi) { + maxi = sum; + index = lvl; + } + } + return index; + } +}; diff --git a/README.md b/README.md @@ -423,6 +423,7 @@ for solving problems. | 1140 | Medium | [Stone Game II](Problems/1140.cpp) | | 1143 | Medium | [Longest Common Subsequence](Problems/1143.cpp) | | 1146 | Medium | [Snapshot Array](Problems/1146.cpp) | +| 1161 | Medium | [Maximum Level Sum of a Binary Tree](Problems/1161.cpp) | | 1162 | Medium | [As Far from Land as Possible](Problems/1162.cpp) | | 1202 | Medium | [Smallest String With Swaps](Problems/1202.cpp) | | 1209 | Medium | [Remove All Adjacent Duplicates in String II](Problems/1209.cpp) |