-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2415-reverse-odd-levels-of-binary-tree.py
More file actions
37 lines (29 loc) · 1.06 KB
/
2415-reverse-odd-levels-of-binary-tree.py
File metadata and controls
37 lines (29 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# time complexity: O(n)
# space complexity: O(logn)
from typing import Optional
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def reverseOddLevels(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
def traverse(leftChild: Optional[TreeNode], rightChild: Optional[TreeNode], level: int):
if not leftChild or not rightChild:
return None
if level % 2 == 0:
temp = leftChild.val
leftChild.val = rightChild.val
rightChild.val = temp
traverse(leftChild.left, rightChild.right, level + 1)
traverse(leftChild.right, rightChild.left, level + 1)
traverse(root.left, root.right, 0)
return root
root = TreeNode(2)
root.left = TreeNode(3)
root.right = TreeNode(5)
root.left.left = TreeNode(8)
root.left.right = TreeNode(13)
root.right.left = TreeNode(21)
root.right.right = TreeNode(34)
print(Solution().reverseOddLevels(root))