Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion core/core.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
#pragma once
#include<iostream>
#include<vector>
#include <queue>
#include<string>

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:
Expand All @@ -24,4 +36,49 @@ class Core
}
}
}

static void printBinaryTreeInLevelOrder(TreeNode* root)
{
if (!root)
{
cout << "\n";
return;
}

queue<TreeNode*> q;
q.push(root);
vector<string> 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";
}
};
117 changes: 117 additions & 0 deletions src/2100-2199/2196.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <sstream>
#include <limits>
#include <iomanip>
#include <functional>
#include <cstring>
#include <climits>
#include "../../core/core.h"
using namespace std;

class Solution {
private:
unordered_map<int, TreeNode*> nodeMap;
unordered_set<int> childSet;
public:
TreeNode* createBinaryTree(vector<vector<int>>& 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<vector<int>> 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;
}
19 changes: 19 additions & 0 deletions tests/2100-2199/2196.txt
Original file line number Diff line number Diff line change
@@ -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
Loading