Bird
Raised Fist0
DSA Goprogramming~20 mins

Check if Two Trees are Symmetric in DSA Go - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of symmetric check for two identical trees
What is the output of the following Go code that checks if two binary trees are symmetric?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}

func main() {
    tree1 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 3}}
    tree2 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 3}}
    fmt.Println(isSymmetric(tree1, tree2))
}
Atrue
BCompilation error
CRuntime panic
Dfalse
Attempts:
2 left
💡 Hint
Check if the trees are mirror images, not just identical.
❓ Predict Output
intermediate
2:00remaining
Output when one tree is nil and the other is not
What does the following Go code print when checking symmetry between a nil tree and a non-nil tree?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}

func main() {
    var tree1 *TreeNode = nil
    tree2 := &TreeNode{Val: 1}
    fmt.Println(isSymmetric(tree1, tree2))
}
ACompilation error
BRuntime panic
Cfalse
Dtrue
Attempts:
2 left
💡 Hint
One tree is empty, the other is not.
🔧 Debug
advanced
2:00remaining
Identify the error in symmetric tree check code
What error will the following Go code produce when run?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Left) && isSymmetric(t1.Right, t2.Right)
}

func main() {
    tree1 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 3}}
    tree2 := &TreeNode{Val: 1, Left: &TreeNode{Val: 3}, Right: &TreeNode{Val: 2}}
    fmt.Println(isSymmetric(tree1, tree2))
}
Afalse
Btrue
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Check the recursive calls for symmetry logic.
🧠 Conceptual
advanced
1:30remaining
Understanding mirror symmetry in binary trees
Which statement best describes the condition for two binary trees to be symmetric?
ABoth trees must have the same structure and node values in the same positions.
BThe left subtree of one tree must be a mirror reflection of the right subtree of the other tree, and vice versa.
CBoth trees must have the same number of nodes regardless of structure.
DThe sum of all node values in both trees must be equal.
Attempts:
2 left
💡 Hint
Think about mirror reflection, not just equality.
❓ Predict Output
expert
2:30remaining
Output of symmetric check on complex trees
What is the output of the following Go code that checks if two complex binary trees are symmetric?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}

func main() {
    tree1 := &TreeNode{
        Val: 1,
        Left: &TreeNode{
            Val: 2,
            Left: &TreeNode{Val: 3},
            Right: &TreeNode{Val: 4},
        },
        Right: &TreeNode{
            Val: 2,
            Left: &TreeNode{Val: 4},
            Right: &TreeNode{Val: 3},
        },
    }
    tree2 := &TreeNode{
        Val: 1,
        Left: &TreeNode{
            Val: 2,
            Left: &TreeNode{Val: 4},
            Right: &TreeNode{Val: 3},
        },
        Right: &TreeNode{
            Val: 2,
            Left: &TreeNode{Val: 3},
            Right: &TreeNode{Val: 4},
        },
    }
    fmt.Println(isSymmetric(tree1, tree2))
}
Atrue
Bfalse
CCompilation error
DRuntime panic
Attempts:
2 left
💡 Hint
Check mirror positions of nodes carefully.

Practice

(1/5)
1. What does it mean for two binary trees to be symmetric?
easy
A. They are mirror images of each other around their center.
B. They have the same number of nodes.
C. They have the same root value only.
D. They have identical left subtrees only.

Solution

  1. Step 1: Understand symmetry in trees

    Two trees are symmetric if one is the mirror reflection of the other around the center.
  2. Step 2: Compare structure and node values

    Both structure and node values must match in a mirrored way for symmetry.
  3. Final Answer:

    They are mirror images of each other around their center. -> Option A
  4. Quick Check:

    Symmetry means mirror image [OK]
Hint: Symmetric trees mirror each other exactly [OK]
Common Mistakes:
  • Thinking same number of nodes means symmetric
  • Only comparing root values
  • Ignoring structure differences
2. Which Go function signature correctly represents a function to check if two trees are symmetric?
easy
A. func isSymmetric(t1 *TreeNode) bool
B. func isSymmetric(t1 TreeNode, t2 TreeNode) int
C. func isSymmetric(t1 *TreeNode, t2 *TreeNode) bool
D. func isSymmetric(t1 *TreeNode, t2 *TreeNode) string

Solution

  1. Step 1: Identify correct parameter types

    Function should take pointers to TreeNode structs for both trees to compare.
  2. Step 2: Identify correct return type

    Return type should be bool to indicate symmetric (true) or not (false).
  3. Final Answer:

    func isSymmetric(t1 *TreeNode, t2 *TreeNode) bool -> Option C
  4. Quick Check:

    Pointer params and bool return [OK]
Hint: Use pointers and bool return for tree comparison [OK]
Common Mistakes:
  • Using value instead of pointer for trees
  • Returning int or string instead of bool
  • Missing one tree parameter
3. What will be the output of the following Go code snippet?
package main
import "fmt"

type TreeNode struct {
    Val int
    Left, Right *TreeNode
}

func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Right) && isSymmetric(t1.Right, t2.Left)
}

func main() {
    root1 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 2}}
    root2 := &TreeNode{Val: 1, Left: &TreeNode{Val: 2}, Right: &TreeNode{Val: 3}}
    fmt.Println(isSymmetric(root1, root2))
}
medium
A. true
B. false
C. Compilation error
D. Runtime panic

Solution

  1. Step 1: Compare root nodes

    Both roots have value 1, so check children.
  2. Step 2: Compare left and right subtrees

    Left subtree of root1 has value 2, right subtree of root2 has value 3, which differ.
  3. Final Answer:

    false -> Option B
  4. Quick Check:

    Different child values mean not symmetric [OK]
Hint: Check mirrored child values for symmetry [OK]
Common Mistakes:
  • Assuming same root means symmetric
  • Ignoring mismatch in child nodes
  • Confusing symmetric with identical
4. Identify the bug in the following Go function that checks if two trees are symmetric:
func isSymmetric(t1, t2 *TreeNode) bool {
    if t1 == nil && t2 == nil {
        return true
    }
    if t1 == nil || t2 == nil {
        return false
    }
    return t1.Val == t2.Val && isSymmetric(t1.Left, t2.Left) && isSymmetric(t1.Right, t2.Right)
}
medium
A. The function does not check root node values.
B. The base case for nil nodes is missing.
C. The function should return int, not bool.
D. The recursive calls compare wrong child nodes for symmetry.

Solution

  1. Step 1: Check recursive calls for symmetry

    Symmetry requires comparing left of one tree with right of the other, but code compares left with left and right with right.
  2. Step 2: Identify correct recursive calls

    Correct calls should be isSymmetric(t1.Left, t2.Right) and isSymmetric(t1.Right, t2.Left).
  3. Final Answer:

    The recursive calls compare wrong child nodes for symmetry. -> Option D
  4. Quick Check:

    Mirror children must be compared [OK]
Hint: Compare left with right child for symmetry [OK]
Common Mistakes:
  • Comparing left with left and right with right
  • Missing nil base cases
  • Returning wrong type
5. Given two binary trees, write a Go function to check if they are symmetric. Which approach correctly handles the edge case where one tree is nil and the other is not?
hard
A. Return false immediately if one tree is nil and the other is not.
B. Return true if one tree is nil and the other is not.
C. Ignore nil checks and compare node values only.
D. Return true if both trees have the same number of nodes.

Solution

  1. Step 1: Handle nil tree edge cases

    If one tree is nil and the other is not, they cannot be symmetric, so return false immediately.
  2. Step 2: Ensure correct base cases in recursion

    Only return true if both trees are nil at the same time, otherwise false.
  3. Final Answer:

    Return false immediately if one tree is nil and the other is not. -> Option A
  4. Quick Check:

    Nil mismatch means not symmetric [OK]
Hint: Check nil mismatch first to avoid errors [OK]
Common Mistakes:
  • Returning true when one tree is nil
  • Skipping nil checks causing runtime errors
  • Comparing only node counts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard