Published:
Updated:

  • Reference
  • dfs λ°©μ‹μœΌλ‘œ ν’€μ—ˆλ‹€. (μž¬κ·€)
  • 파이참이 λ§ˆμ§€λ§‰μ€„μ—μ„œ λ‘λ²ˆμ§ΈμΈ self.answer 뢀뢄에 밑쀄을 μ³μ„œ hyunμ—κ²Œ 도움을 μš”μ²­ν–ˆλ‹€.
    • 결둠은 속도도 κ·Έλ ‡κ³  if문이 μ•ˆ 돌면 self.answerκ°€ 생성이 μ•ˆ λ˜λ‹ˆ μ•žμœΌλ‘œλŠ” κΌ­ μƒμ„±ν•΄μ£Όμž!
  • μƒμ„±μž X
    • Runtime 632 ms
    • Beats 43.13%
  • μƒμ„±μž O
    • Runtime 624 ms
    • Beats 52.38%
  • μ‹œκ°„λ³΅μž‘λ„: $O(N)$


Solution

# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    # μΆ”κ°€λœ μƒμ„±μž
    def __init__(self):
        self.answer = None

    def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
        self.inorder(original, cloned, target)
        return self.answer

    def inorder(self, original: TreeNode, cloned: TreeNode, target: TreeNode):
        if original:
            self.inorder(original.left, cloned.left, target)
            if original is target:
                self.answer = cloned
            self.inorder(original.right, cloned.right, target)

Leave a comment