/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{privateTreeNodetarget;publicfinalTreeNodegetTargetCopy(finalTreeNodeoriginal,finalTreeNodecloned,finalTreeNodetarget){this.target=target;returndfs(original,cloned);}privateTreeNodedfs(TreeNoderoot1,TreeNoderoot2){if(root1==null){returnnull;}if(root1==target){returnroot2;}TreeNoderes=dfs(root1.left,root2.left);returnres==null?dfs(root1.right,root2.right):res;}}
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */publicclassSolution{privateTreeNodetarget;publicTreeNodeGetTargetCopy(TreeNodeoriginal,TreeNodecloned,TreeNodetarget){this.target=target;returndfs(original,cloned);}privateTreeNodedfs(TreeNodeoriginal,TreeNodecloned){if(original==null){returnnull;}if(original==target){returncloned;}TreeNodeleft=dfs(original.left,cloned.left);returnleft==null?dfs(original.right,cloned.right):left;}}