-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1372.py
More file actions
44 lines (34 loc) · 1.31 KB
/
1372.py
File metadata and controls
44 lines (34 loc) · 1.31 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
38
39
40
41
42
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def longestZigZag(self, root):
"""
:type root: TreeNode
:rtype: int
"""
# visited = defaultdict(lambda: 0)
if not root:
return 0
def alternate_traverse(curr_node, prev_turn, curr_depth):
curr_temp_node, curr_len = curr_node, curr_depth
if curr_temp_node is None:
return curr_depth
else:
if prev_turn == 'L':
depth = max(
curr_depth,
alternate_traverse(curr_temp_node.right, 'R', curr_len+1),
alternate_traverse(curr_temp_node.left, 'L', 0)
)
if prev_turn == 'R':
depth = max(
curr_depth,
alternate_traverse(curr_temp_node.left, 'L', curr_len+1),
alternate_traverse(curr_temp_node.right, 'R', 0)
)
return depth
return max(alternate_traverse(root.left, 'L', 0), alternate_traverse(root.right, 'R', 0))