leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0112.cpp (593B)
0 class Solution { 1 public: 2 bool hasPathSum(TreeNode *root, int targetSum) { 3 if (!root) return false; 4 5 stack<pair<TreeNode *, int>> st; 6 st.push(make_pair(root, 0)); 7 while (!st.empty()) { 8 TreeNode *root = st.top().first; 9 int val = st.top().second + root->val; 10 st.pop(); 11 12 if (val == targetSum && !root->left && !root->right) return true; 13 14 if (root->left) st.push(make_pair(root->left, val)); 15 if (root->right) st.push(make_pair(root->right, val)); 16 } 17 return false; 18 } 19 };