Grokking Microsoft Coding Interview
Ask Author
Back to course home

0% completed

Vote For New Content
Stack/iterative version c++

lejafilip

Aug 27, 2024

static bool hasPath(TreeNode *root, int sum) {     if(!root)       return false;       std::stack<std::pair<TreeNode*, int>> nodes;     nodes.push({root, sum - root->val});     while(!nodes.empty())     {       auto node = nodes.top();       nodes.pop();       if(node.second == 0 && !node.first->left && !node.first->right)       {         return true;       }       if(node.first->left)         nodes.push({node.first->left, node.second - node.first->left->val});             if(node.first->right)         nodes.push({node.first->right, node.second - node.first->right->val});     }     return false;   }

0

0

Comments
Comments

On this page