Very Simple level order Traversal

interview-questions
Tags: #<Tag:0x00007f1825348480>

#1
int Solution::solve(TreeNode* A) {
    if(!A) return 0;
    int mx=0;
    queue<TreeNode*> q;
    q.push(A);
    while(!q.empty()){
        int n=q.size(),s=0;
        while(n--){
            A=q.front();
            q.pop();
            s+=A->val;
            if(A->left) q.push(A->left);
            if(A->right) q.push(A->right);
        }
        mx=max(mx,s);
    }
    return mx;
}