当前位置:网站首页>LeetCode每日一题(2196. Create Binary Tree From Descriptions)
LeetCode每日一题(2196. Create Binary Tree From Descriptions)
2022-07-28 11:53:00 【wangjun861205】
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 <= 104
- descriptions[i].length == 3
- 1 <= parenti, childi <= 105
- 0 <= isLefti <= 1
- The binary tree described by descriptions is valid.
找到根节点,然后根据关系从上向下构建整棵树
use std::cell::RefCell;
use std::collections::{
HashMap, HashSet};
use std::rc::Rc;
impl Solution {
fn build(root: i32, children: &HashMap<i32, Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
if root == -1 {
return None;
}
let mut node = TreeNode::new(root);
if let Some(c) = children.get(&root) {
node.left = Solution::build(c[0], children);
node.right = Solution::build(c[1], children);
}
Some(Rc::new(RefCell::new(node)))
}
pub fn create_binary_tree(descriptions: Vec<Vec<i32>>) -> Option<Rc<RefCell<TreeNode>>> {
let mut children = HashMap::new();
let mut root = HashSet::new();
let mut removed = HashSet::new();
for desc in descriptions {
children.entry(desc[0]).or_insert(vec![-1, -1])[(desc[2] - 1).abs() as usize] = desc[1];
if !removed.contains(&desc[0]) {
root.insert(desc[0]);
}
root.remove(&desc[1]);
removed.insert(desc[1]);
}
if root.len() != 1 {
unreachable!()
}
for r in root {
return Solution::build(r, &children);
}
unreachable!()
}
}
边栏推荐
- Brief introduction to JS operator
- Zurich Federal Institute of technology | reference based image super resolution with deformable attention transformer (eccv2022))
- 【嵌入式C基础】第5篇:原码/反码/补码
- scala 转换、过滤、分组、排序
- [June 28 event preview] low code Summit
- LeetCode 移除元素&移动零
- Merge table rows - three levels of for loop traversal data
- Pits encountered in MSP430 development (to be continued)
- 面试必问,敲重点!讲一下 Android Application 启动流程及其源码?
- Phpstudy steps to quickly build a website (teach you to build it by hand)
猜你喜欢

云原生—运行时环境

Redefinition problem of defining int i variable in C for loop

Leetcode:704 binary search

Leetcode 42. rainwater connection

连通块&&食物链——(并查集小结)

Connected Block & food chain - (summary of parallel search set)

Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property

2020-12-07

Summary: idea problem record
![[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32](/img/c3/4268d7e4e1429f9b0d9d928790d740.png)
[graduation design] smart home system based on ZigBee - single chip microcomputer Internet of things stm32
随机推荐
Leetcode:704 binary search
Solution to using json.tojsonstring to display question marks in Chinese in Servlet
01 introduction to pyechars features, version and installation
DART 三维辐射传输模型申请及下载
Connected Block & food chain - (summary of parallel search set)
Redefinition problem of defining int i variable in C for loop
大模型哪家强?OpenBMB发布BMList给你答案!
Block reversal (summer vacation daily question 7)
2020-12-27
.NET的求复杂类型集合的差集、交集、并集
Unity loads GLB model
机器学习基础-贝叶斯分析-14
Leetcode 1518. wine change
QT signal and slot mechanism (detailed)
Siemens docking Leuze BPS_ 304i notes
Li FuPan: application practice of kata safety container in ant group
【嵌入式C基础】第5篇:原码/反码/补码
05 pyechars basic chart (example code + effect diagram)
子线程更新UI全解
MySQL limit paging optimization