straightforward C++ BFS


  • 0
    D
    vector<int> largestValues(TreeNode* root) {
        std::vector<int> res;
        if (!root) return res;
        std::queue<TreeNode*> queue;
        queue.push(root);
        while(queue.size() > 0) {
            int size = queue.size();
            int max = INT_MIN;
            for (int i = 0 ; i < size ; i++) {
                TreeNode* node = queue.front();
                max = std::max(max, node->val);
                queue.pop();
                if (node->left) queue.push(node->left);
                if (node->right) queue.push(node->right);
            }
            res.push_back(max);
        }
        
        return res;
    }

Log in to reply
 

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