C++ Solution


  • 0
    Z

    C++ Solution

    class Solution {
    public:
        vector<int> largestValues(TreeNode* root) {
            vector<int> res;
            if(!root) return res;
            queue<TreeNode*> q; q.push(root);
            TreeNode* last = root; int max = INT_MIN;
            while(!q.empty()) {
            	TreeNode* cur = q.front();
            	q.pop();
            	if(cur->val>max) max = cur->val;
            	if(cur->left) q.push(cur->left);
            	if(cur->right) q.push(cur->right);
            	if(cur == last) {
            		last = q.back();
            		res.push_back(max);
            		max = INT_MIN;
            	}
            }
            return res;
        }
    };
    

Log in to reply
 

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