Time limit exceeded?

  • 0

    I don't understand why my solution is timing out (takes 120-130ms) - my understanding is since we are counting number of nodes, we should be visiting all the nodes. Can we do better than this? Am I missing anything?

    class Solution {
    int countNodes(TreeNode* root) {
    if (root == NULL) {
    return 0;
    return 1 + countNodes(root->left) + countNodes(root->right);

  • 0

    large time complexity?

Log in to reply

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