Please tell me what's wrong with my code


  • 0
    Z
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     struct TreeNode *left;
     *     struct TreeNode *right;
     * };
     */
    /**
     * Return an array of size *returnSize.
     * Note: The returned array must be malloced, assume caller calls free().
     */
    int height;
    void dfs(int,int re[],struct TreeNode * root,int);
    int * rightSideView(struct TreeNode *root,int * returnSize)
    {
            int * re=(int *)malloc(sizeof(int)*(*returnSize));
            height=0;
            dfs(1,re,root,*returnSize);
            return re;
    }
    void dfs(int i,int re[],struct TreeNode*pt,int returnSize)
    {
            
            if(pt)
            {
                if(i>height&&i<=returnSize){
                        re[i-1]=pt->val;
                        height++;
    
                }
                
                dfs(i+1,re,pt->right,returnSize);
                dfs(i+1,re,pt->left,returnSize);
                
            }
    }

  • 0
    D

    You misunderstand the usage of returnSize. Please check the initial comment part:

    Return an array of size *returnSize.

    That is, you should use returnSize to tell OJ the size of your returned result.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.