From 9f03fe922497a18314ae3dc0e7e07c80aa7605db Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Wed, 25 Dec 2024 22:51:29 +0530 Subject: [PATCH] Create 515. Find Largest Value in Each Tree Row1 --- 515. Find Largest Value in Each Tree Row1 | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 515. Find Largest Value in Each Tree Row1 diff --git a/515. Find Largest Value in Each Tree Row1 b/515. Find Largest Value in Each Tree Row1 new file mode 100644 index 0000000..2d471c1 --- /dev/null +++ b/515. Find Largest Value in Each Tree Row1 @@ -0,0 +1,23 @@ +class Solution { +public: + vector largestValues(TreeNode* root) { + queue q; + if(root==NULL) return {}; + q.push(root); + vector res; + while (!q.empty()) { + int n = q.size(), maxi = INT_MIN; + for (int i = 0; i < n; i++) { + TreeNode* node = q.front(); + q.pop(); + maxi = max(maxi, node->val); + if (node->left) + q.push(node->left); + if (node->right) + q.push(node->right); + } + res.push_back(maxi); + } + return res; + } +};