LeetCode 0101. Symmetric Tree Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0101. Symmetric Tree

Description

Given the root of a binary tree, check whether it is a mirror of itself (i.e., symmetric around its center).

 

Example 1:

Input: root = [1,2,2,3,4,4,3]
Output: true

Example 2:

Input: root = [1,2,2,null,3,null,3]
Output: false

 

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -100 <= Node.val <= 100

 

Follow up: Could you solve it both recursively and iteratively?

Solutions

Solution 1: Recursion

We design a function dfs(root1, root2) to determine whether two binary trees are symmetric. The answer is dfs(root.left, root.right).

The logic of the function dfs(root1, root2) is as follows:

  • If both root1 and root2 are null, the two binary trees are symmetric, and we return true;
  • If only one of root1 and root2 is null, or root1.valroot2.val, we return false;
  • Otherwise, we check whether the left subtree of root1 is symmetric with the right subtree of root2, and whether the right subtree of root1 is symmetric with the left subtree of root2, using recursion.

The time complexity is O(n), and the space complexity is O(n). Here, n is the number of nodes in the binary tree.

PythonJavaC++GoTypeScriptRustJavaScript
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: def dfs(root1: Optional[TreeNode], root2: Optional[TreeNode]) -> bool: if root1 == root2: return True if root1 is None or root2 is None or root1.val != root2.val: return False return dfs(root1.left, root2.right) and dfs(root1.right, root2.left) return dfs(root.left, root.right)(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !