We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2394d5b commit 2db6996Copy full SHA for 2db6996
invert-binary-tree/yyyyyyyyyKim.py
@@ -0,0 +1,30 @@
1
+# Definition for a binary tree node.
2
+# class TreeNode:
3
+# def __init__(self, val=0, left=None, right=None):
4
+# self.val = val
5
+# self.left = left
6
+# self.right = right
7
+class Solution:
8
+ def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
9
+
10
+ # 이진트리 뒤집기(좌우반전)
11
+ # DFS(stack이용, 시간복잡도 O(n), 공간복잡도 O(n))
12
+ if not root:
13
+ return None
14
15
+ stack = [root]
16
17
+ while stack:
18
+ # 스택 맨뒤에 있는 거 pop해서 꺼내기
19
+ node = stack.pop()
20
21
+ # 자식 노드 교환
22
+ node.left, node.right = node.right, node.left
23
24
+ # 자식 노드를 스택에 추가
25
+ if node.left:
26
+ stack.append(node.left)
27
+ if node.right:
28
+ stack.append(node.right)
29
30
+ return root
0 commit comments