Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions invert-binary-tree/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
# Time Complexity: O(N) / Space Complexity: O(N)
if not root or (not root.left and not root.right):
return root

def invert(node: TreeNode):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invert() 메서드를 분리해서 DFS 방식으로 풀이하신게 맞으실까요? 혹시 invert() 메서드를 따로 정의하지 않고 invertTree() 메서드만 사용해서 풀이할 수도 있을 것 같은데 어떻게 생각하시나요?

if not node:
return
node.right, node.left = node.left, node.right
if node.right:
invert(node.right)
if node.left:
invert(node.left)

invert(root)
return root