mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
leetcode 还没有题,自己构建
This commit is contained in:
@ -1528,6 +1528,54 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
pub struct Solution;
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub struct Node {
|
||||||
|
pub val: i32,
|
||||||
|
pub children: Vec<Option<Rc<RefCell<Node>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<Rc<RefCell<Node>>>) -> Vec<Vec<i32>> {
|
||||||
|
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.在每个树行中找最大值
|
# 515.在每个树行中找最大值
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
|
[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
|
||||||
|
Reference in New Issue
Block a user