leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
1379.cpp (656B)
0 class Solution {
1 public:
2 TreeNode *getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target) {
3 if (!original || !cloned || !target) return nullptr;
5 stack<pair<TreeNode *, TreeNode *>> st;
7 st.push({original, cloned});
8 while (!st.empty()) {
9 TreeNode *original = st.top().first;
10 TreeNode *cloned = st.top().second;
11 st.pop();
13 if (original == target) return cloned;
15 if (original->left) st.push({original->left, cloned->left});
16 if (original->right) st.push({original->right, cloned->right});
17 }
19 return nullptr;
20 }
21 };