Symmetric Tree Solutions in Python
Number 101
Difficulty Easy
Acceptance 46.9%
Link LeetCode
Solutions
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/symmetric-tree/# Author : penpenps# Time : 2019-07-09# Revert right node firstly, then compare with left node# Time Complexity: O(n)# Space Complexity: O(n)# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def isSymmetric(self, root: TreeNode) -> bool:# Revert TreeNodedef revert(node):if not node: returnnode.left, node.right = node.right, node.leftrevert(node.left)revert(node.right)def isEqual(left, right):if not left and not right: return Trueif not left or not right: return Falseif left.val != right.val:return Falsereturn isEqual(left.left, right.left) and isEqual(left.right, right.right)if not root:return Truerevert(root.right)return isEqual(root.left, root.right)
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/symmetric-tree/# Author : penpenps# Time : 2019-07-09# Recursively check whether the child node is symmetric or not# Time Complexity: O(n)# Space Complexity: O(n)# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def isSymmetric(self, root: TreeNode) -> bool:# Check whether left and right child are symmeticdef symmetricEqual(left, right):# left and right child are both None, return Trueif not left and not right: return True# one of left and right is None, return Falseif not left or not right: return False# the value of left and right are not equal, return Falseif left.val != right.val:return Falsereturn symmetricEqual(left.left, right.right) and symmetricEqual(left.right, right.left)if not root:return Truereturn symmetricEqual(root.left, root.right)
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/symmetric-tree/# Author : penpenps# Time : 2019-07-09# Level traversal using two queues to keep left and right sub-tree# Time Complexity: O(n)# Space Complexity: O(n)# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def isSymmetric(self, root: TreeNode) -> bool:if not root:return Trueq1, q2 = [root], [root]while q1 and q2:n1, n2 = q1.pop(), q2.pop()if not n1 and not n2:continueif not n1 or not n2:return Falseif n1.val != n2.val:return Falseq1.append(n1.left)q1.append(n1.right)q2.append(n2.right)q2.append(n2.left)return not q1 and not q2
Python solution by liuyubobobo/Play-Leetcode
# Source : https://leetcode.com/problems/symmetric-tree/# Author : penpenps# Time : 2019-07-09# Use single queue to store left and right subtree elements in different directions# Time Complexity: O(n)# Space Complexity: O(n)# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def isSymmetric(self, root: TreeNode) -> bool:if not root:return Trueq = [root, root]while q:n1, n2 = q.pop(), q.pop()if not n1 and not n2:continueif not n1 or not n2:return Falseif n1.val != n2.val:return Falseq.append(n1.left)q.append(n2.right)q.append(n1.right)q.append(n2.left)return True