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:

在这里插入图片描述

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.

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;
}

相关推荐

  1. KY199 查找

    2024-01-20 23:40:01       44 阅读
  2. _198打家劫舍

    2024-01-20 23:40:01       60 阅读
  3. Leetcode--198

    2024-01-20 23:40:01       40 阅读
  4. Leetcode 169

    2024-01-20 23:40:01       44 阅读
  5. 199_二叉树的右视图

    2024-01-20 23:40:01       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-20 23:40:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-20 23:40:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-20 23:40:01       87 阅读
  4. Python语言-面向对象

    2024-01-20 23:40:01       96 阅读

热门阅读

  1. 科技创新推动绿色能源革命

    2024-01-20 23:40:01       63 阅读
  2. 开发安全之:Path Manipulation

    2024-01-20 23:40:01       65 阅读
  3. Netty和传统NIO之间的比较

    2024-01-20 23:40:01       56 阅读
  4. git提权

    git提权

    2024-01-20 23:40:01      59 阅读
  5. 力扣labuladong——一刷day95

    2024-01-20 23:40:01       53 阅读
  6. 鸿蒙开发语言ArkTS--Ability中的生命周期

    2024-01-20 23:40:01       74 阅读
  7. 算子开发参考

    2024-01-20 23:40:01       64 阅读