LeetCode 题解工作台
根据描述创建二叉树
给你一个二维整数数组 descriptions ,其中 descriptions[i] = [parent i , child i , isLeft i ] 表示 parent i 是 child i 在 二叉树 中的 父节点 ,二叉树中各节点的值 互不相同 。此外: 如果 isLeft i == …
4
题型
7
代码语言
3
相关题
当前训练重点
中等 · 数组·哈希·扫描
答案摘要
我们可以用一个哈希表 来存储所有节点,其中键为节点的值,值为节点本身,用一个集合 来存储所有的子节点。 遍历 ,对于每个描述 $[\textit{parent}, \textit{child}, \textit{isLeft}]$,如果 不在 中,我们就将 加入 ,并初始化一个值为 的节点。如果 不在 中,我们就将 加入 ,并初始化一个值为 的节点。然后我们将 加入 。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 数组·哈希·扫描 题型思路
题目描述
给你一个二维整数数组 descriptions ,其中 descriptions[i] = [parenti, childi, isLefti] 表示 parenti 是 childi 在 二叉树 中的 父节点,二叉树中各节点的值 互不相同 。此外:
- 如果
isLefti == 1,那么childi就是parenti的左子节点。 - 如果
isLefti == 0,那么childi就是parenti的右子节点。
请你根据 descriptions 的描述来构造二叉树并返回其 根节点 。
测试用例会保证可以构造出 有效 的二叉树。
示例 1:

输入:descriptions = [[20,15,1],[20,17,0],[50,20,1],[50,80,0],[80,19,1]] 输出:[50,20,80,15,17,19] 解释:根节点是值为 50 的节点,因为它没有父节点。 结果二叉树如上图所示。
示例 2:

输入:descriptions = [[1,2,1],[2,3,0],[3,4,1]] 输出:[1,2,null,null,3,4] 解释:根节点是值为 1 的节点,因为它没有父节点。 结果二叉树如上图所示。
提示:
1 <= descriptions.length <= 104descriptions[i].length == 31 <= parenti, childi <= 1050 <= isLefti <= 1descriptions所描述的二叉树是一棵有效二叉树
解题思路
方法一:哈希表
我们可以用一个哈希表 来存储所有节点,其中键为节点的值,值为节点本身,用一个集合 来存储所有的子节点。
遍历 ,对于每个描述 ,如果 不在 中,我们就将 加入 ,并初始化一个值为 的节点。如果 不在 中,我们就将 加入 ,并初始化一个值为 的节点。然后我们将 加入 。
如果 为真,我们就将 作为 的左子节点,否则我们就将 作为 的右子节点。
最后,我们遍历 ,如果某个节点的值不在 中,那么这个节点就是根节点,我们返回这个节点。
时间复杂度 ,空间复杂度 。其中 是 的长度。
# 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]:
nodes = defaultdict(TreeNode)
children = set()
for parent, child, isLeft in descriptions:
if parent not in nodes:
nodes[parent] = TreeNode(parent)
if child not in nodes:
nodes[child] = TreeNode(child)
children.add(child)
if isLeft:
nodes[parent].left = nodes[child]
else:
nodes[parent].right = nodes[child]
root = (set(nodes.keys()) - children).pop()
return nodes[root]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | O(n) |
| 空间 | O(n) |
面试官常问的追问
外企场景- question_mark
Can the candidate handle efficient tree construction using hash maps?
- question_mark
Does the candidate identify the root node correctly without extra checks?
- question_mark
How well does the candidate optimize space and time complexity while solving the problem?
常见陷阱
外企场景- error
Overcomplicating the identification of the root node, which should be straightforward by excluding children.
- error
Not utilizing hash maps effectively, which can lead to inefficient lookups when constructing the tree.
- error
Confusing left and right child assignments, especially if the index or flag is misinterpreted.
进阶变体
外企场景- arrow_right_alt
What if the tree had additional constraints, such as balanced or full binary trees?
- arrow_right_alt
How would the solution change if the parent-child descriptions were in a different format?
- arrow_right_alt
How can we optimize the space complexity if we were asked to process multiple trees simultaneously?