share my straightforward C++ solution


  • 0
    W

    vector<int> largestValues(TreeNode* root) {

    vector<int> rslt;
    if (!root) return rslt;
    queue<TreeNode*> this_level;
    this_level.push(root);
    while (!this_level.empty()) {
    
    	queue<TreeNode*> next_level;
    	int largest = INT_MIN;
    	while (!this_level.empty()) {
    
    		TreeNode* node = this_level.front();
    		this_level.pop();
    		largest = max(largest, node->val);
    		if (node->left) next_level.push(node->left);
    		if (node->right) next_level.push(node->right);
    
    	}
    	rslt.push_back(largest);
    	this_level = next_level;
    
    }
    return rslt;
    

    }


Log in to reply
 

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