c++ clear code and use the queue


  • 0
    9
    public:
        vector<int> largestValues(TreeNode* root) {
            vector<int> result;
            if(!root) return result;
            queue<TreeNode*> q;
            q.push(root);
            int levelmax=q.front()->val;
            while(!q.empty())
            {
                int levelmax=q.front()->val;
                int count=q.size();
                for(int i=0;i<count;i++)
                {
                    if(q.front()->val>levelmax) levelmax=q.front()->val;
                    if(q.front()->left) q.push(q.front()->left);
                    if(q.front()->right) q.push(q.front()->right);
                    q.pop();
                }
                result.push_back(levelmax);
            }
            return result;
        }
    };

Log in to reply
 

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