forked from partho-maple/coding-interview-gym
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy path101_Symmetric_Tree.py
60 lines (43 loc) · 1.4 KB
/
101_Symmetric_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
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
# Iterative solution, Wrong answer but not sure why
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None:
return True
stack = [(root.left, root.right)]
while stack:
left, right = stack.pop()
if left is None and right is None:
return True
if left is None or right is None:
return False
if left.val != right.val:
return False
stack.append((left.left, right.right))
stack.append((left.right, right.left))
return True
# Recursive solution
class Solution(object):
def isSymmetric(self, root):
"""
:type root: TreeNode
:rtype: bool
"""
if root is None:
return True
return self.isMirrorByDFS(root.left, root.right)
def isMirrorByDFS(self, node1, node2):
if node1 is None and node2 is None:
return True
if node1 is None or node2 is None:
return False
return node1.val == node2.val and self.isMirrorByDFS(node1.left, node2.right) and self.isMirrorByDFS(node2.left, node1.right)