c++ solution


  • 0
    W
    class Solution {
    public:
        vector<int> largestValues(TreeNode* root) {
            vector<int> res;
    		if (root==NULL) return res;
    		queue<TreeNode*> q;
    		q.push(root);
    		while (!q.empty()) {
    			int len = q.size();
    			int largest = INT_MIN;
    			for (int i=0; i<len; ++i) {
    				TreeNode* cur = q.front();
    				q.pop();
    				if (cur->val > largest) largest = cur->val;
    				if (cur->left) q.push(cur->left);
    				if (cur->right) q.push(cur->right);
    			}
    			res.push_back(largest);
    		}
    		return res;
        }
    };
    

Log in to reply
 

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