Grokking the Coding Interview: Patterns for Coding Questions
Vote

0% completed

​

Solution: Binary Tree Path Sum

.....

.....

.....

Like the course? Get enrolled and start learning!
A

Anonymous

· 2 years ago

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;   }
A

Anonymous

· 4 years ago

how does the or work in the return statement? Are they both run simultaneously?

Show 1 reply

Reading Progress

0%


Vote for new content