[
tree
dfs
recursion
]
BinarySearch 0293 Symmetric Binary Tree
Problem statement
https://binarysearch.com/problems/Symmetric-Binary-Tree/
Solution
Equal to Leetcode 0101 Symmetric Tree.
Complexity
It is O(n)
for time and O(h)
for space.
Code
class Solution:
def solve(self, root):
def dfs(n1, n2):
if not n1 and not n2: return True
if not n1 or not n2: return False
return n1.val == n2.val and dfs(n1.left, n2.right) and dfs(n1.right, n2.left)
if not root: return True
return dfs(root.left, root.right)