Challenge - 5 Problems
Symmetry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
Check if the trees are mirror images, not just identical.
✗ Incorrect
The function checks if two trees are mirror images. The given trees are identical but not mirrors because left and right children do not match in mirrored positions.
❓ Predict Output
intermediate2: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)) }
Attempts:
2 left
💡 Hint
One tree is empty, the other is not.
✗ Incorrect
If one tree is nil and the other is not, they cannot be symmetric.
🔧 Debug
advanced2: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)) }
Attempts:
2 left
💡 Hint
Check the recursive calls for symmetry logic.
✗ Incorrect
The code compares left with left and right with right, which checks for equality, not mirror symmetry.
🧠 Conceptual
advanced1:30remaining
Understanding mirror symmetry in binary trees
Which statement best describes the condition for two binary trees to be symmetric?
Attempts:
2 left
💡 Hint
Think about mirror reflection, not just equality.
✗ Incorrect
Symmetry means one tree is a mirror image of the other, so left subtree of one matches right subtree of the other.
❓ Predict Output
expert2: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)) }
Attempts:
2 left
💡 Hint
Check mirror positions of nodes carefully.
✗ Incorrect
The trees are mirror images of each other with matching node values in mirrored positions.