Simple C solution dfs: 0ms


  • 0
    S
    int solution(struct TreeNode *root, int *res, int *cur_res, int top, int cur_lv, int *lv)
    {
        int i;
        
        if (root == NULL) {
            if (cur_lv > *lv) {
                for (i = top; i < (top + (cur_lv - *lv)); i++) {
                  res[i] = cur_res[i];
                }
                *lv = cur_lv;
                top = i;
            }
            
            return top;
        }
        
        cur_res[cur_lv] = root->val;
        top = solution(root->right, res, cur_res, top, cur_lv + 1, lv);
        
        cur_res[cur_lv] = root->val;
        top = solution(root->left, res, cur_res, top, cur_lv + 1, lv);
        
        return top;
    }
    int* rightSideView(struct TreeNode* root, int* returnSize) {
        int level = 0;
        
        int *res = malloc (sizeof(int) * 50);
        int *cur_res = malloc (sizeof(int) * 50);
    
        *returnSize = solution(root, res, cur_res, 0, 0, &level);
        
        return res;
    }
    

Log in to reply
 

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