199, 二叉树的右视图
大约 4 分钟
一、题目描述
给定一个二叉树的根节点root
,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
示例 1
输入: root = [1, 2, 3, null, 5, null, 4]
输出: [1, 3, 4]
示例 2
输入: root = [1, null, 3]
输出: [1, 3]
示例 3
输入: root = []
输出: []
提示
- 二叉树的节点个数的范围是
[0,100]
-100 <= Node.val <= 100
相关主题
- 树
- 深度优先搜索
- 广度优先搜索
- 二叉树
二、题解
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None,
}
}
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
方法 1: 深度优先搜索
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
//Self::dfs_recursion(root)
//Self::dfs_iteration_1(root)
//Self::dfs_iteration_2(root)
Self::dfs_iteration_3(root)
}
///
/// 深度优先搜索 - 递归实现
///
fn dfs_recursion(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
const RECURSION_IMPL: fn(Option<Rc<RefCell<TreeNode>>>, usize, &mut Vec<i32>) =
|root, level, res| {
if let Some(curr) = root {
if level == res.len() {
res.push(curr.borrow().val);
}
// go Right
RECURSION_IMPL(curr.borrow_mut().right.take(), level + 1, res);
// go Left
RECURSION_IMPL(curr.borrow_mut().left.take(), level + 1, res);
}
};
RECURSION_IMPL(root, 0, &mut res);
res
}
///
/// 深度优先搜索 - 迭代实现
///
fn dfs_iteration_1(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut root = (root, 0);
let mut stack = vec![];
while root.0.is_some() || !stack.is_empty() {
match root.0 {
Some(curr) => {
let curr_level = root.1;
if res.len() == curr_level {
res.push(curr.borrow().val);
}
root = (curr.borrow_mut().right.take(), curr_level + 1);
stack.push((curr, curr_level));
}
None => {
if let Some((curr, curr_level)) = stack.pop() {
root = (curr.borrow_mut().left.take(), curr_level + 1);
}
}
}
}
res
}
///
/// 深度优先搜索 - 迭代实现
///
fn dfs_iteration_2(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut root = (root, 0);
let mut stack = vec![];
while root.0.is_some() || !stack.is_empty() {
while let Some(curr) = root.0 {
let curr_level = root.1;
if res.len() == curr_level {
res.push(curr.borrow().val);
}
root = (curr.borrow_mut().right.take(), curr_level + 1);
stack.push((curr, curr_level));
}
if let Some((curr, curr_level)) = stack.pop() {
root = (curr.borrow_mut().left.take(), curr_level + 1);
}
}
res
}
///
/// 深度优先搜索 - 迭代实现
///
fn dfs_iteration_3(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
if let Some(root) = root {
let mut stack = vec![Ok((root, 0))];
while let Some(root) = stack.pop() {
match root {
Ok((curr, curr_level)) => {
// Left
if let Some(left) = curr.borrow_mut().left.take() {
stack.push(Ok((left, curr_level + 1)));
}
// Right
if let Some(right) = curr.borrow_mut().right.take() {
stack.push(Ok((right, curr_level + 1)));
}
// Root
stack.push(Err((curr.borrow().val, curr_level)));
}
Err((val, curr_level)) => {
if res.len() == curr_level {
res.push(val);
}
}
}
}
}
res
}
public List<Integer> rightSideView(TreeNode root) {
//return this.dfsRecursion(root);
//return this.dfsIteration1(root);
//return this.dfsIteration2(root);
return this.dfsIteration3(root);
}
@FunctionalInterface
interface TriConsumer<X, Y, Z> {
void accept(X x, Y y, Z z);
}
TriConsumer<TreeNode, Integer, List<Integer>> recurImpl = (root, level, res) -> {
if (root == null) {
return;
}
if (level == res.size()) {
res.add(root.val);
}
if (root.right != null) {
this.recurImpl.accept(root.right, level + 1, res);
}
if (root.left != null) {
this.recurImpl.accept(root.left, level + 1, res);
}
};
/**
* 深度优先搜索 - 递归实现
*/
List<Integer> dfsRecursion(TreeNode _root) {
List<Integer> res = new ArrayList<>();
this.recurImpl.accept(_root, 0, res);
return res;
}
/**
* 深度优先搜索 - 迭代实现
*/
List<Integer> dfsIteration1(TreeNode _root) {
List<Integer> res = new ArrayList<>();
Deque<Object[]> stack = new ArrayDeque<>();
Object[] root = new Object[]{_root, 0};
while (root[0] != null || !stack.isEmpty()) {
TreeNode curr = (TreeNode) root[0];
int curr_level = (int) root[1];
if (curr != null) {
if (curr_level == res.size()) {
res.add(curr.val);
}
stack.push(new Object[]{curr, curr_level});
// go Right
root = new Object[]{curr.right, curr_level + 1};
} else {
Object[] top = stack.pop();
TreeNode top_node = (TreeNode) top[0];
int top_level = (int) top[1];
root = new Object[]{top_node.left, top_level + 1};
}
}
return res;
}
/**
* 深度优先搜索 - 迭代实现
*/
List<Integer> dfsIteration2(TreeNode _root) {
List<Integer> res = new ArrayList<>();
Deque<Object[]> stack = new ArrayDeque<>();
Object[] root = new Object[]{_root, 0};
while (root[0] != null || !stack.isEmpty()) {
while (root[0] != null) {
TreeNode curr = (TreeNode) root[0];
int curr_level = (int) root[1];
if (curr_level == res.size()) {
res.add(curr.val);
}
stack.push(new Object[]{curr, curr_level});
// go Right
root = new Object[]{curr.right, curr_level + 1};
}
Object[] top = stack.pop();
TreeNode top_node = (TreeNode) top[0];
int top_level = (int) top[1];
root = new Object[]{top_node.left, top_level + 1};
}
return res;
}
/**
* 深度优先搜索 - 迭代实现
*/
List<Integer> dfsIteration3(TreeNode _root) {
List<Integer> res = new ArrayList<>();
if (_root != null) {
Deque<Object[]> stack = new ArrayDeque<>() {{
this.push(new Object[]{_root, 0});
}};
while (!stack.isEmpty()) {
Object[] pop = stack.pop();
if (pop instanceof Integer[] vals) {
if (vals[1] == res.size()) {
res.add(vals[0]);
}
} else {
TreeNode curr = (TreeNode) pop[0];
int curr_level = (int) pop[1];
// Left
if (curr.left != null) {
stack.push(new Object[]{curr.left, curr_level + 1});
}
// Right
if (curr.right != null) {
stack.push(new Object[]{curr.right, curr_level + 1});
}
// Root
stack.push(new Integer[]{curr.val, curr_level});
}
}
}
return res;
}
方法 2: 广度优先搜索
pub fn right_side_view(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
//Self::bfs_iteration_1(root)
Self::bfs_iteration_2(root)
}
///
/// 广度优先搜索 - 迭代实现
///
fn bfs_iteration_1(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
if let Some(root) = root {
let mut queue = VecDeque::from([root]);
while !queue.is_empty() {
let level_len = queue.len();
for i in 1..=level_len {
if let Some(curr) = queue.pop_front() {
// if you enqueue left node first, here should be i == level_len
// if you enqueue right node first, here should be i == 0
if i == level_len {
res.push(curr.borrow().val);
}
if let Some(left) = curr.borrow_mut().left.take() {
queue.push_back(left);
}
if let Some(right) = curr.borrow_mut().right.take() {
queue.push_back(right);
}
}
}
}
}
res
}
///
/// 广度优先搜索 - 迭代实现
///
fn bfs_iteration_2(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
if let Some(root) = root {
let mut queue = VecDeque::from([(root, 0)]);
while let Some((curr, curr_level)) = queue.pop_front() {
if curr_level == res.len() {
res.push(curr.borrow().val);
}
if let Some(right) = curr.borrow_mut().right.take() {
queue.push_back((right, curr_level + 1));
}
if let Some(left) = curr.borrow_mut().left.take() {
queue.push_back((left, curr_level + 1));
}
}
}
res
}
public List<Integer> rightSideView(TreeNode root) {
//return this.bfsIteration1(root);
return this.bfsIteration2(root);
}
/**
* 广度优先搜索 - 迭代实现
*/
List<Integer> bfsIteration1(TreeNode _root) {
List<Integer> res = new ArrayList<>();
if (_root != null) {
Deque<TreeNode> queue = new ArrayDeque<>() {{
this.addLast(_root);
}};
while (!queue.isEmpty()) {
int levelSize = queue.size();
for (int i = 1; i <= levelSize; i++) {
TreeNode curr = queue.removeFirst();
// if you enqueue left node first, here should be i == levelSize
// if you enqueue right node first, here should be i == 0
if (i == levelSize) {
res.add(curr.val);
}
if (curr.left != null) {
queue.addLast(curr.left);
}
if (curr.right != null) {
queue.addLast(curr.right);
}
}
}
}
return res;
}
/**
* 广度优先搜索 - 迭代实现
*/
List<Integer> bfsIteration2(TreeNode _root) {
List<Integer> res = new ArrayList<>();
if (_root != null) {
Deque<Object[]> queue = new ArrayDeque<>() {{
this.addLast(new Object[]{_root, 0});
}};
while (!queue.isEmpty()) {
Object[] pop = queue.removeFirst();
TreeNode curr = (TreeNode) pop[0];
int curr_level = (int) pop[1];
if (curr_level == res.size()) {
res.add(curr.val);
}
if (curr.right != null) {
queue.addLast(new Object[]{curr.right, curr_level + 1});
}
if (curr.left != null) {
queue.addLast(new Object[]{curr.left, curr_level + 1});
}
}
}
return res;
}