Created
December 15, 2022 23:36
-
-
Save abdulrahmanAlotaibi/486296ed18b4bbe187608d8f15010338 to your computer and use it in GitHub Desktop.
Tree: Symmetric Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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