leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0653.cpp (661B)


0 class Solution {
1 TreeNode *root;
2 TreeNode *find(int k) {
3 TreeNode *t = root;
4 while (t && t->val != k)
5 t = t->val > k ? t->left : t->right;
6 return t;
7 }
9 public:
10 bool findTarget(TreeNode *root, int k) {
11 stack<TreeNode *> st;
12 st.push(this->root = root);
13 while (!st.empty()) {
14 TreeNode *t, *root = st.top();
15 st.pop();
16 while (root) {
17 if ((t = find(k - root->val)) && t != root) return true;
18 if (root->right) st.push(root->right);
19 root = root->left;
20 }
21 }
22 return false;
23 }
24 };