My test result is ok,but the submit result (the same test case) is wrong, why?


  • 0
    H

    build a queue with an array,front and back

    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
            vector<int> v;
            if(root==NULL) return v;
            int levels[1000];
            TreeNode *q[1000]={0};
            int front=0,back=0;
            levels[0]=1;
            q[back++]=root;
            while(front!=back){
                TreeNode *lt=q[front]->left;
                if(lt){
                    levels[back]=levels[front]+1;
                    q[back++]=lt;
                }
                TreeNode *rt=q[front]->right;
                if(rt) {
                    levels[back]=levels[front]+1;
                    q[back++]=rt;
                }
                front++;
            }
            for(int i=0;i<front;i++){
                if(levels[i]!=levels[i+1]){
                    v.push_back(q[i]->val);
                }
            }
            return v;
        }
    };
    

    the test case [1,null,3,,5,null,null,4,null].it fails when submited ,but tested ok in test run!


Log in to reply
 

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