Skip to content

Instantly share code, notes, and snippets.

@abdulrahmanAlotaibi
Created December 15, 2022 23:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abdulrahmanAlotaibi/486296ed18b4bbe187608d8f15010338 to your computer and use it in GitHub Desktop.
Save abdulrahmanAlotaibi/486296ed18b4bbe187608d8f15010338 to your computer and use it in GitHub Desktop.
Tree: Symmetric Tree
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSymmetric(root *TreeNode) bool {
q := []*TreeNode{root,root}
for len(q) > 0{
t1 := q[0]
t2 := q[1]
q = q[2:]
if t1 == nil && t2 == nil {
continue
}
if t1 == nil || t2 == nil {
return false
}
if t1.Val != t2.Val {
return false
}
q = append(q, t1.Left)
q = append(q, t2.Right)
q = append(q, t1.Right)
q = append(q, t2.Left)
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment