跳至主要內容

199, 二叉树的右视图

Mike大约 4 分钟binary treemediumbinary treedepth first searchbreadth first search

一、题目描述

给定一个二叉树的根节点root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。

示例 1
5 nodes
输入: 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,
        }
    }
}

方法 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
}

方法 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
}