/**
* Problem: LeetCode 2196 - Create Binary Tree From Descriptions
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.*;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
/**
* Problem: LeetCode 2196 - Create Binary Tree From Descriptions
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public TreeNode createBinaryTree(int[][] descriptions) {
Map<Integer, TreeNode> map = new HashMap<>();
Set<Integer> children = new HashSet<>();
for (int[] d : descriptions) {
int parent = d[0];
int child = d[1];
int isLeft = d[2];
map.putIfAbsent(parent, new TreeNode(parent));
map.putIfAbsent(child, new TreeNode(child));
TreeNode parentNode = map.get(parent);
TreeNode childNode = map.get(child);
if (isLeft == 1) {
parentNode.left = childNode;
} else {
parentNode.right = childNode;
}
children.add(child);
}
for (int[] d : descriptions) {
int parent = d[0];
if (!children.contains(parent)) {
return map.get(parent);
}
}
return null;
}
}
"""
Problem: LeetCode 2196 - Create Binary Tree From Descriptions
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List, Optional
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def createBinaryTree(self, descriptions: List[List[int]]) -> Optional[TreeNode]:
node_map = {}
children = set()
for parent, child, is_left in descriptions:
if parent not in node_map:
node_map[parent] = TreeNode(parent)
if child not in node_map:
node_map[child] = TreeNode(child)
if is_left == 1:
node_map[parent].left = node_map[child]
else:
node_map[parent].right = node_map[child]
children.add(child)
for parent, _, _ in descriptions:
if parent not in children:
return node_map[parent]
return None
/**
* Problem: LeetCode 2196 - Create Binary Tree From Descriptions
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <unordered_map>
#include <unordered_set>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* 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) {}
* };
*/
/**
* Problem: LeetCode 2196 - Create Binary Tree From Descriptions
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
TreeNode* createBinaryTree(vector<vector<int>>& descriptions) {
unordered_map<int, TreeNode*> nodeMap;
unordered_set<int> children;
for (const auto& d : descriptions) {
int parent = d[0];
int child = d[1];
int isLeft = d[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 == 1) {
nodeMap[parent]->left = nodeMap[child];
} else {
nodeMap[parent]->right = nodeMap[child];
}
children.insert(child);
}
for (const auto& d : descriptions) {
int parent = d[0];
if (children.find(parent) == children.end()) {
return nodeMap[parent];
}
}
return nullptr;
}
};
/**
* Problem: LeetCode 2196 - Create Binary Tree From Descriptions
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {number[][]} descriptions
* @return {TreeNode}
*/
var createBinaryTree = function(descriptions) {
const map = new Map();
const children = new Set();
for (const [parent, child, isLeft] of descriptions) {
if (!map.has(parent)) {
map.set(parent, new TreeNode(parent));
}
if (!map.has(child)) {
map.set(child, new TreeNode(child));
}
const parentNode = map.get(parent);
const childNode = map.get(child);
if (isLeft === 1) {
parentNode.left = childNode;
} else {
parentNode.right = childNode;
}
children.add(child);
}
for (const [parent] of descriptions) {
if (!children.has(parent)) {
return map.get(parent);
}
}
return null;
};