1379.cpp (656B)
1 class Solution { 2 public: 3 TreeNode *getTargetCopy(TreeNode *original, TreeNode *cloned, TreeNode *target) { 4 if (!original || !cloned || !target) return nullptr; 5 6 stack<pair<TreeNode *, TreeNode *>> st; 7 8 st.push({original, cloned}); 9 while (!st.empty()) { 10 TreeNode *original = st.top().first; 11 TreeNode *cloned = st.top().second; 12 st.pop(); 13 14 if (original == target) return cloned; 15 16 if (original->left) st.push({original->left, cloned->left}); 17 if (original->right) st.push({original->right, cloned->right}); 18 } 19 20 return nullptr; 21 } 22 };