-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_mirror_binary_tree.py
71 lines (55 loc) · 1.5 KB
/
check_mirror_binary_tree.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#
# https://leetcode.com/problems/symmetric-tree/
#
#
# Given a root of binary tree - check whether it is symmetric related to root
#
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# Optimized version:
#
def is_mirror(left, right):
if not left or not right:
return left == right
if left.val != right.val:
return False
return is_mirror(left.left, right.right) and is_mirror(left.right, right.left)
def is_symmetric_v2(root):
return is_mirror(root, root)
#
# Initial, not so optimal version
#
def is_symmetric_v1(root):
def unroll(root, level, cur_array):
if root:
cur_array[level].append(root.val)
unroll(root.left, level + 1, cur_array)
unroll(root.right, level + 1, cur_array)
else:
cur_array[level].append(None)
if not root:
return True
from collections import defaultdict
left = defaultdict(list)
right = defaultdict(list)
level = 2
unroll(root.left, level, left)
unroll(root.right, level, right)
l1 = len(left)
l2 = len(right)
if l1 != l2:
return False
for level in left:
n1 = len(left[level])
n2 = len(right[level])
if n1 != n2:
return False
for idx in xrange(n1):
if left[level][idx] != right[level][n2 - idx - 1]:
return False
return True