LeetCode //C - 199. Binary Tree Right Side View

这篇具有很好参考价值的文章主要介绍了LeetCode //C - 199. Binary Tree Right Side View。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

199. Binary Tree Right Side View

Given the root of a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.
 

Example 1:

LeetCode //C - 199. Binary Tree Right Side View,LeetCode,leetcode,c语言,算法

Input: root = [1,2,3,null,5,null,4]
Output: [1,3,4]

Example 2:

Input: root = [1,null,3]
Output: [1,3]

Example 3:

Input root = []
Output []

Constraints:
  • The number of nodes in the tree is in the range [0, 100].
  • -100 <= Node.val <= 100

From: LeetCode
Link: 199. Binary Tree Right Side View


Solution:

Ideas:

The rightSideView function uses a depth-first search strategy to traverse the tree and record the last value encountered at each depth. This assumes that the tree is being traversed such that the rightmost node at each depth will be the node seen from the right side view. The function then returns an array of these values.文章来源地址https://www.toymoban.com/news/detail-809647.html

Code:
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */

int* rightSideView(struct TreeNode* root, int* returnSize) {
    // If the tree is empty
    if (root == NULL) {
        *returnSize = 0;
        return NULL;
    }

    int depth = 0;
    int capacity = 8;
    int* rightValues = (int*)malloc(capacity * sizeof(int));
    struct TreeNode** stack = (struct TreeNode**)malloc(capacity * sizeof(struct TreeNode*));
    int* depths = (int*)malloc(capacity * sizeof(int));
    
    if (!rightValues || !stack || !depths) {
        // Handle allocation failure if needed
    }

    int stackSize = 0;
    stack[stackSize] = root;
    depths[stackSize++] = 1;

    while (stackSize > 0) {
        struct TreeNode* node = stack[--stackSize];
        int currentDepth = depths[stackSize];

        if (currentDepth > depth) {
            depth = currentDepth;
            if (depth > capacity) {
                capacity *= 2;
                rightValues = (int*)realloc(rightValues, capacity * sizeof(int));
                stack = (struct TreeNode**)realloc(stack, capacity * sizeof(struct TreeNode*));
                depths = (int*)realloc(depths, capacity * sizeof(int));
                
                if (!rightValues || !stack || !depths) {
                    // Handle allocation failure if needed
                }
            }
            rightValues[depth - 1] = node->val;
        }

        if (node->left) {
            stack[stackSize] = node->left;
            depths[stackSize++] = currentDepth + 1;
        }
        
        if (node->right) {
            stack[stackSize] = node->right;
            depths[stackSize++] = currentDepth + 1;
        }
    }

    free(stack);
    free(depths);

    *returnSize = depth;
    return rightValues;
}

到了这里,关于LeetCode //C - 199. Binary Tree Right Side View的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • LeetCode //C - 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.   Example 1: Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] Output: [3,9,20,null,null,15,7] Example 2: Input: preorder = [-1], inorder = [-1] Output: [-

    2024年02月09日
    浏览(32)
  • LeetCode //C - 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree.   Example 1: Input: inorder = [9,3,15,20,7], postorder = [9,15,7,20,3] Output: [3,9,20,null,null,15,7] Example 2: Input: inorder = [-1], postorder = [-1] Outpu

    2024年02月09日
    浏览(35)
  • PAT 1174 Left-View of Binary Tree 题干不知所云

    个人学习记录,代码难免不尽人意。 The left-view of a binary tree is a list of nodes obtained by looking at the tree from left hand side and from top down. For example, given a tree shown by the figure, its left-view is { 1, 2, 3, 4, 5 } Given the inorder and preorder traversal sequences of a binary tree, you are supposed to output its left-vie

    2024年02月09日
    浏览(27)
  • leetcode199. 二叉树的右视图(java)

    来源:力扣(LeetCode) 链接:https://leetcode.cn/problems/binary-tree-right-side-view 给定一个二叉树的 根节点 root,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。 示例1: 输入: [1,2,3,null,5,null,4] 输出: [1,3,4] 示例 2: 输入: [1,null,3] 输出: [1,3] 示例 3: 输入

    2024年02月09日
    浏览(33)
  • 【LeetCode-中等题】199. 二叉树的右视图

    根 右 左 的顺序 取每层第一个被访问到的节点(depth == list.size())

    2024年02月10日
    浏览(34)
  • leetcode分类刷题:二分查找(Binary Search)(四、基于值域的数组/矩阵类型)

    基于值域的二分法与基于定义域的题型不同,它的目标是从一“ 特殊排序序列 ”中确定“第k个元素值”,而不像基于定义域的题型是从排序序列中找小于等于特定target值的第一个索引;同时,针对“特殊排序序列”,往往需要 嵌套使用双指针 法进行操作,进一步增加了对

    2024年02月11日
    浏览(61)
  • Leetcode 1022. Sum of Root To Leaf Binary Numbers (树遍历题)

    Sum of Root To Leaf Binary Numbers Easy 3.3K 183 Companies You are given the root of a binary tree where each node has a value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 - 1 - 1 - 0 - 1, then this could represent 01101 in binary, which is 13. For all leaves in the tree, cons

    2024年02月03日
    浏览(51)
  • Leetcode 1011. Capacity To Ship Packages Within D Days (Binary Search经典)

    Capacity To Ship Packages Within D Days Medium 8.6K 179 Companies A conveyor belt has packages that must be shipped from one port to another within days days. The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may not load more weight than the maxi

    2024年02月09日
    浏览(33)
  • LeetCode每日一题589. N-ary Tree Preorder Traversal

    Given the root of an n-ary tree, return the preorder traversal of its nodes’ values. Nary-Tree input serialization is represented in their level order traversal. Each group of children is separated by the null value (See examples) Example 1: Input: root = [1,null,3,2,4,null,5,6] Output: [1,3,5,6,2,4] Example 2: Input: root = [1,null,2,3,4,5,null,null,6,7,n

    2024年02月19日
    浏览(26)
  • 小白水平理解面试经典题目LeetCode 404 Sum of Left Leaves【Tree】

    给定二叉树的root,返回所有左叶的总和。 叶子是没有子节点的节点。左叶是另一个节点的左子节点的叶。 在大学某个自习的下午,小白坐在教室看到这道题。想想自己曾经和白月光做题,现在大过年的,也是只有自己练题了。左边一颗树,右边一棵树。。。 这时候黑长直女

    2024年02月22日
    浏览(50)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包