[LeetCode] 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
Updated:
Find a Corresponding Node of a Binary Tree in a Clone of That Tree
๋ฌธ์ ๋ฅผ ์ฝ๊ณ ๋์ ? ํ๋ค. ๋ญ๊ฐ origianl tree, cloned tree ํ๋ฉด์ ์์ฒญ ๊ฑฐ์ฐฝํด๋ณด์๋ค.. ๊ทธ๋ฐ๋ฐ ๊ทธ๋ฅ ๋ ธ๋ ์ฐพ๊ธฐ ๋ฌธ์ ์๋ค.
์ ์๋ณธ, ๋ณต์ ํธ๋ฆฌ๋ฅผ ๊ตฌ๋ถ์ง์ ๊ฒ ์ผ๊น.. ๋ฌธ์ ํ์ด์ ์๋ฌด๋ฐ ์๊ด์ด ์๋ค.. ๊ทธ๋ฅ target ๋ ธ๋ ์ฐพ์๊ฐ๋ฉด ๊ทธ๋ง..
๊ทธ๋์ BFS ๋ฅผ ํตํด ์ฐพ์๊ฐ๋ ๋ฐฉ์์ผ๋ก ์ฝ๋ฉ ํ๋ค.
from collections import deque
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def getTargetCopy(self, original: TreeNode, cloned: TreeNode, target: TreeNode) -> TreeNode:
queue = deque()
queue.append(cloned)
while queue:
node = queue.popleft()
if node.val == target.val:
return node
if node.left:
queue.append(node.left)
if node.right:
queue.append(node.right)
๊ทธ๋ฐ๋ฐ ํ๊ณ ๋ณด๋ ๋ฌธ์ ์ ์ข์์ ๋ณด๋ค ์ซ์ด์๊ฐ ๋ ๋ง์๋ ๋ฌธ์ ์๋ค. ๋ค๋ฅธ ์ฌ๋๋ค๋ ๋ณด๊ณ ์๊ฒผ๋๋ณด๋ค!
Leave a comment