오늘 배웠습니다: 이진 트리 반전
3552 단어 pythonalgorithmswebdevbeginners
문제 설명
이진 트리를 가져와 반전시키는 함수를 작성하십시오.
샘플 입력
tree = 1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
샘플 결과
tree = 1
/ \
3 2
/ \ / \
7 6 5 4
/ \
9 8
코드 #1
def invert_binary_tree(tree):
queue = [tree]
while len(queue) > 0:
cur_node = queue.pop(0)
if cur_node is not None:
cur_node.left, cur_node.right = cur_node.right, cur_node.left
queue.append(cur_node.left)
queue.append(cur_node.right)
return tree
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
메모
tree = 1
/ \
2 3
/ \ / \
4 5 6 7
/ \
8 9
샘플 결과
tree = 1
/ \
3 2
/ \ / \
7 6 5 4
/ \
9 8
코드 #1
def invert_binary_tree(tree):
queue = [tree]
while len(queue) > 0:
cur_node = queue.pop(0)
if cur_node is not None:
cur_node.left, cur_node.right = cur_node.right, cur_node.left
queue.append(cur_node.left)
queue.append(cur_node.right)
return tree
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
메모
tree = 1
/ \
3 2
/ \ / \
7 6 5 4
/ \
9 8
def invert_binary_tree(tree):
queue = [tree]
while len(queue) > 0:
cur_node = queue.pop(0)
if cur_node is not None:
cur_node.left, cur_node.right = cur_node.right, cur_node.left
queue.append(cur_node.left)
queue.append(cur_node.right)
return tree
# This is the class of the input binary tree.
class BinaryTree:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
메모
크레딧
Reference
이 문제에 관하여(오늘 배웠습니다: 이진 트리 반전), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/anzhari/today-i-learned-invert-binary-tree-hcj텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)