199. Binary Tree Right Side View(二叉树的右视图)

这是一篇关于解决LeetCode上题目199的博客,主要探讨如何找到二叉树的右视图。提供了两种方法,Approach1利用层序遍历,仅保留每层的最后一个节点;Approach2的核心思想是每层只选择一个节点,视图深度等于结果列表的当前大小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

在这里插入图片描述

题目链接

https://leetcode.com/problems/binary-tree-right-side-view/

方法思路

Apprach1:
基于层序遍历,只添加每层的最后一个节点的值。

class Solution {
    //Runtime: 1 ms, faster than 72.32%
    //Memory Usage: 37.1 MB, less than 86.24%
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> ans = new LinkedList<>();
        if(root == null) return ans;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> list = new LinkedList<>();
            while(size-- > 0){
                TreeNode node = queue.poll();
                list.add(node.val);
                if(node.left != null) queue.offer(node.left);
                if(node.right != null) queue.offer(node.right);
            }
            ans.add(list.get(list.size() - 1));
        }
        return ans;
    }
}

Apprach2:
The core idea of this algorithm:
1.Each depth of the tree only select one node.
2. View depth is current size of result list.

public class Solution {
    //Runtime: 1 ms, faster than 72.32%
    //Memory Usage: 37.3 MB, less than 27.53%
    public List<Integer> rightSideView(TreeNode root) {
        List<Integer> result = new ArrayList<Integer>();
        rightView(root, result, 0);
        return result;
    }
    
    public void rightView(TreeNode curr, List<Integer> result, int currDepth){
        if(curr == null){
            return;
        }
        if(currDepth == result.size()){
            result.add(curr.val);
        }
        
        rightView(curr.right, result, currDepth + 1);
        rightView(curr.left, result, currDepth + 1);
        
    }
}
在输出二叉树右视图问题中,给定的是前序遍历(根-左-右)和中序遍历(左-根-右),我们需要找到每个层级上右侧节点的最大值。这种问题通常通过构建两个辅助栈来解决,一个用于存储当前层的节点,另一个用于跟踪中序遍历中的节点。 首先,你需要理解前序遍历的头节点对应于中序遍历中的根节点。接下来,遍历中序遍历,当遇到前序遍历的第一个节点时,开始比较前序遍历中剩余部分与中序遍历之间的节点数量。如果右部节点多,则记录下当前节点;如果左部节点多,继续在左侧寻找下一个右视图。最后,当遍历结束后,栈顶的元素就是右视图序列。 以下是Java代码示例: ```java import java.util.Stack; public class RightViewOfBinaryTree { public int[] rightSideView(TreeNode root) { if (root == null) return new int[0]; Stack<TreeNode> preorder = new Stack<>(); Stack<Integer> inorder = new Stack<>(); TreeNode prevPreorderNode = null; TreeNode currInorderNode = root; // Preorder traversal while (!preorder.isEmpty() || currInorderNode != null) { if (currInorderNode != null) { inorder.push(currInorderNode); currInorderNode = currInorderNode.left; } else { TreeNode topInorder = inorder.pop(); if (prevPreorderNode != null && topInorder.val > prevPreorderNode.val) { // Find the first node with greater value on the right side while (!inorder.isEmpty() && inorder.peek().val <= prevPreorderNode.val) { inorder.pop(); } if (!inorder.isEmpty()) { prevPreorderNode = topInorder; } } preorder.push(currInorderNode); currInorderNode = currInorderNode.right; } } // Convert indexes to values and store in an array int[] result = new int[preorder.size()]; for (int i = 0; i < result.length; i++) { result[i] = preorder.get(i).val; } return result; } // Helper class for binary tree nodes static class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int val) { this.val = val; } } } ``` 在这个代码中,`rightSideView`函数接收一个二叉树的根节点作为输入,并返回一个表示右视图的整数数组。注意这里假设了`TreeNode`类的存在,这是一个常见的二叉树节点定义。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值