We say that a binary tree is symmetric if it is made up of exactly similar items facing each other around the root of the binary tree. In other words, suppose we draw vertical line that start from the root node, the tree is symmetric if all nodes with the same distance from this line in the same level are equal.
Let's some examples of a symmetric binary tree
The tree | Symmetric | Why? |
---|---|---|
Yes | 2=2 3=3 4=4 |
|
Yes | 2=2 4=4 X=X |
|
Yes | 2=2 X=X 3=3 |
|
No | 5 != 4 | |
No | 2 != 5 |
Suppose we have the following class
class Node:
def __init__(self,data:int,left:'Node',right:'Node'):
self.data = data
self.left = left
self.right = right
Write a function, that check the binary tree is symmetric
def is_symmetric( root : Node) -> bool:
pass
Write a function, that check the binary tree is symmetric with constant memory
def is_symmetricA( data : List[int,None] ) -> bool:
pass