0
0
DSA Goprogramming~20 mins

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

Choose your learning style9 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.