-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path101SymmetricTree.py
60 lines (52 loc) · 1.68 KB
/
101SymmetricTree.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 9 21:04:34 2019
@author: alaric
"""
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
# Recursive
def dfs(left , right):
if not left and not right:
return True
if not left or not right:
return False
if left.val != right.val:
return False
return dfs(left.left , right.right) or dfs(left.right, right.left)
if root == None:
return True
return dfs(root.left ,root.right)
def isSymmetric(self, root: TreeNode) -> bool:
# Iterative
queue = [root , root]
while any(queue):
t1 = queue.pop()
t2 = queue.pop()
if t1 == None and t2 == None:
continue
if t1 == None or t2 == None:
return False
if t1.val != t2.val:
return False
queue.append(t1.left)
queue.append(t2.right)
queue.append(t1.right)
queue.append(t2.left)
return True
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def dfs(left,right):
if left == None and right == None:
return True
elif left == None or right == None:
return False
return left.val == right.val and dfs(left.left , right.right) and dfs(left.right , right.left)
return dfs(root , root)