Search⌘ K
AI Features

Symmetric Tree

Explore how to check whether a binary tree is symmetric around its root by using breadth-first search techniques. Understand the concept of tree symmetry and practice implementing a solution to verify if a tree is a mirror of itself, improving your ability to solve common coding interview problems involving tree traversals.

Statement

Given the root of a binary tree, check whether it is a symmetric tree. A symmetric tree refers to a tree that is a mirror of itself, i.e., symmetric around its root.

Constraints:

  • The tree contains nodes in the range [1,500][1, 500].
  • 103-10^3 \le Node.data 103\le 10^3

Examples

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Symmetric Tree

1.

What will be the output if the following tree is given as input?

tree = [25, 4, 67, 2, 3, 3, 2]

          __ 25 __ 
         |        |
       _ 4 _    _ 67 _ 
      |     |  |      |
      2     3  3      2 
       
A.

TRUE

B.

FALSE


1 / 3

Figure it out!

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5
6
7

Try it yourself

Implement your solution in main.go in the following coding playground. We have provided a useful code template in the other file that you may build on to solve this problem.

Go
usercode > main.go
// Definition of a binary tree node
// type TreeNode[T any] struct {
// Data T
// Left *TreeNode[T]
// Right *TreeNode[T]
// }
package main
import (. "golang-test-code/ds_v1/BinaryTree") // TreeNode[T]
func isSymmetric(root *TreeNode[int]) bool{
// Replace this placeholder return statement with your code
return false
}
Symmetric Tree