Created
January 23, 2022 03:48
-
-
Save CollinShoop/e16518af3352e4e2c0b28cb5d0adc290 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/symmetric-tree/ -- Definitely seems more like a medium
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 { | |
if root == nil { | |
return true | |
} | |
var helper func(left *TreeNode, right *TreeNode) bool | |
helper = func(left *TreeNode, right *TreeNode) bool { | |
if left == nil || right == nil { | |
// both nil or wrong | |
return left == nil && right == nil | |
} else if left.Val != right.Val { | |
return false | |
} | |
return helper(left.Left, right.Right) && helper(left.Right, right.Left) | |
} | |
return helper(root.Left, root.Right) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment