From 8b8d56d1029af082bd9ea74f69b248da4e96c225 Mon Sep 17 00:00:00 2001 From: Debashis Nandi Date: Mon, 8 Jun 2026 01:08:16 +0530 Subject: [PATCH] feat: POTD 2026-06-07 solved --- core/core.h | 59 +++++++++++++++++++- src/2100-2199/2196.cpp | 117 +++++++++++++++++++++++++++++++++++++++ tests/2100-2199/2196.txt | 19 +++++++ 3 files changed, 194 insertions(+), 1 deletion(-) create mode 100644 src/2100-2199/2196.cpp create mode 100644 tests/2100-2199/2196.txt diff --git a/core/core.h b/core/core.h index 4641029..76bf0d8 100644 --- a/core/core.h +++ b/core/core.h @@ -1,10 +1,22 @@ #pragma once #include #include +#include #include - using namespace std; +class TreeNode +{ +public: + int val; + TreeNode* left; + TreeNode* right; + TreeNode() : val(0), left(nullptr), right(nullptr) {} + TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + TreeNode(int x, TreeNode* left, TreeNode* right) : val(x), left(left), right(right) {} + +}; + class Core { private: @@ -24,4 +36,49 @@ class Core } } } + + static void printBinaryTreeInLevelOrder(TreeNode* root) + { + if (!root) + { + cout << "\n"; + return; + } + + queue q; + q.push(root); + vector result; + + // Standard BFS Traversal + while (!q.empty()) + { + TreeNode* current = q.front(); + q.pop(); + + if (current) + { + result.push_back(to_string(current->val)); + // Push children even if they are null (clean trailing nulls later) + q.push(current->left); + q.push(current->right); + } + else + { + result.push_back("null"); + } + } + + // Clean up trailing "null"s to match LeetCode's compact format + while (!result.empty() && result.back() == "null") + { + result.pop_back(); + } + + // Print the output + for (int i = 0; i < result.size(); i++) + { + cout << result[i] << " "; + } + cout << "\n"; + } }; \ No newline at end of file diff --git a/src/2100-2199/2196.cpp b/src/2100-2199/2196.cpp new file mode 100644 index 0000000..b80c805 --- /dev/null +++ b/src/2100-2199/2196.cpp @@ -0,0 +1,117 @@ +/* +Tags +level-medium +tree, binary-tree, hash-map, + + +Problem Description +2196. Create Binary Tree From Descriptions +You are given a 2D integer array descriptions where descriptions[i] = [parenti, childi, isLefti] indicates that parenti is the parent of childi in a binary tree of unique values. Furthermore, +If isLefti == 1, then childi is the left child of parenti. +If isLefti == 0, then childi is the right child of parenti. +Construct the binary tree described by descriptions and return its root. + +The test cases will be generated such that the binary tree is valid. + +Example 1: +Input: descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]] +Output: [50,20,80,15,17,19] +Explanation: The root node is the node with value 50 since it has no parent. +The resulting binary tree is shown in the diagram. + +Example 2: +Input: descriptions = [[1,2,1],[2,3,0],[3,4,1]] +Output: [1,2,null,null,3,4] +Explanation: The root node is the node with value 1 since it has no parent. +The resulting binary tree is shown in the diagram. + +Constraints: +1 <= descriptions.length <= 10^4 +descriptions[i].length == 3 +1 <= parenti, childi <= 10^5 +0 <= isLefti <= 1 +The binary tree described by descriptions is valid. + +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../../core/core.h" +using namespace std; + +class Solution { +private: + unordered_map nodeMap; + unordered_set childSet; +public: + TreeNode* createBinaryTree(vector>& descriptions) { + for (auto& it : descriptions) + { + int parent = it[0]; + int child = it[1]; + bool isLeft = it[2]; + if (nodeMap.find(parent) == nodeMap.end()) + { + nodeMap[parent] = new TreeNode(parent); + } + if (nodeMap.find(child) == nodeMap.end()) + { + nodeMap[child] = new TreeNode(child); + } + if (isLeft) + { + nodeMap[parent]->left = nodeMap[child]; + } + else + { + nodeMap[parent]->right = nodeMap[child]; + } + childSet.insert(child); + } + + for (auto& it : nodeMap) + { + auto& val = it.first; + auto& node = it.second; + if (childSet.find(val) == childSet.end()) + { + return node; + } + } + + return nullptr; + } +}; + +int main() +{ + int n; + Solution sol; + vector> description; + cin >> n; + for (int i = 0; i < n; i++) + { + int parent, child, isLeft; + cin >> parent >> child >> isLeft; + description.push_back({parent, child, isLeft}); + } + TreeNode* root = sol.createBinaryTree(description); + Core::printBinaryTreeInLevelOrder(root); + return 0; +} \ No newline at end of file diff --git a/tests/2100-2199/2196.txt b/tests/2100-2199/2196.txt new file mode 100644 index 0000000..4215506 --- /dev/null +++ b/tests/2100-2199/2196.txt @@ -0,0 +1,19 @@ +Test Case 1: +Input: +5 +20 15 1 +20 17 0 +50 20 1 +50 80 0 +80 19 1 +Output: +50 20 80 15 17 19 + +Test Case 2: +Input: +3 +1 2 1 +2 3 0 +3 4 1 +Output: +1 2 null null 3 4 \ No newline at end of file