Skip to content

Latest commit

 

History

History
43 lines (36 loc) · 1018 Bytes

File metadata and controls

43 lines (36 loc) · 1018 Bytes

BFS Method

vector<int> largestValues(TreeNode* root) {
        vector<int> ans;
        queue<TreeNode*>q;

        if(root==NULL)
        {
            //ans.push_back(INT_MIN);
            return ans;
        }

        q.push(root);
        

        while(!q.empty())
        {
            int s=q.size();
            int maxi=INT_MIN;

            for(int i=0;i<s;i++)
            {
                TreeNode* top=q.front();
                q.pop();
                maxi=max(maxi,top->val);            //findinf max at the time of popping
                if(top->left!=NULL)
                {
                    q.push(top->left);
                }
                if(top->right!=NULL)
                {
                    q.push(top->right);
                }
            }
            //if(maxi!=INT_MIN)
                ans.push_back(maxi);
        }
    return ans;
    }