- 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