diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index acd91c6b..1a01c0ae 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -1528,6 +1528,54 @@ object Solution { } ``` +rust: + +```rust +pub struct Solution; +#[derive(Debug, PartialEq, Eq)] +pub struct Node { + pub val: i32, + pub children: Vec>>>, +} + +impl Node { + #[inline] + pub fn new(val: i32) -> Node { + Node { + val, + children: vec![], + } + } +} + +use std::cell::RefCell; +use std::collections::VecDeque; +use std::rc::Rc; +impl Solution { + pub fn level_order(root: Option>>) -> Vec> { + let mut res = vec![]; + let mut queue = VecDeque::new(); + if root.is_some() { + queue.push_back(root); + } + while !queue.is_empty() { + let mut temp = vec![]; + for _ in 0..queue.len() { + let node = queue.pop_front().unwrap().unwrap(); + temp.push(node.borrow().val); + if !node.borrow().children.is_empty() { + for n in node.borrow().children.clone() { + queue.push_back(n); + } + } + } + res.push(temp) + } + res + } +} +``` + # 515.在每个树行中找最大值 [力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)