【leetcode】199. 二叉树的右视图

 

/**
 * 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().
 */

void preorder(struct TreeNode* root, int *returnArr, int height, int* returnSize) {
    if(!root) {
        return;
    }
    if(height + 1 > returnSize[0]) {
        returnSize[0] = height + 1;
    }
    returnArr[height] = root->val;
    preorder(root->left, returnArr, height + 1, returnSize);
    preorder(root->right, returnArr, height + 1, returnSize);
    return;
}
int* rightSideView(struct TreeNode* root, int* returnSize){
    int *returnArr = (int*)calloc(1000, sizeof(int));
    returnSize[0] = 0;
    preorder(root, returnArr, 0, returnSize);
    return returnArr;
}
。

 

posted @ 2020-12-25 22:22  温暖了寂寞  阅读(105)  评论(0编辑  收藏  举报