mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
Update 0102.二叉树的层序遍历.md
This commit is contained in:
@ -1758,6 +1758,38 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
rust:
|
||||||
|
|
||||||
|
```rust
|
||||||
|
use std::cell::RefCell;
|
||||||
|
use std::collections::VecDeque;
|
||||||
|
use std::rc::Rc;
|
||||||
|
impl Solution {
|
||||||
|
pub fn largest_values(root: Option<Rc<RefCell<TreeNode>>>) -> 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 max = i32::MIN;
|
||||||
|
for _ in 0..queue.len() {
|
||||||
|
let node = queue.pop_front().unwrap().unwrap();
|
||||||
|
max = max.max(node.borrow().val);
|
||||||
|
if node.borrow().left.is_some() {
|
||||||
|
queue.push_back(node.borrow().left.clone());
|
||||||
|
}
|
||||||
|
if node.borrow().right.is_some() {
|
||||||
|
queue.push_back(node.borrow().right.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
res.push(max);
|
||||||
|
}
|
||||||
|
res
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
# 116.填充每个节点的下一个右侧节点指针
|
# 116.填充每个节点的下一个右侧节点指针
|
||||||
|
|
||||||
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
|
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
|
||||||
|
Reference in New Issue
Block a user