mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 00:25:22 +08:00
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
|
|
// 解法一 dfs
|
|
func isSymmetric(root *TreeNode) bool {
|
|
if root == nil {
|
|
return true
|
|
}
|
|
return isMirror(root.Left, root.Right)
|
|
}
|
|
|
|
func isMirror(left *TreeNode, right *TreeNode) bool {
|
|
if left == nil && right == nil {
|
|
return true
|
|
}
|
|
if left == nil || right == nil {
|
|
return false
|
|
}
|
|
return (left.Val == right.Val) && isMirror(left.Left, right.Right) && isMirror(left.Right, right.Left)
|
|
}
|
|
|
|
// 解法二
|
|
func isSymmetric1(root *TreeNode) bool {
|
|
if root == nil {
|
|
return true
|
|
}
|
|
return isSameTree(invertTree(root.Left), root.Right)
|
|
}
|
|
|
|
func isSameTree(p *TreeNode, q *TreeNode) bool {
|
|
if p == nil && q == nil {
|
|
return true
|
|
} else if p != nil && q != nil {
|
|
if p.Val != q.Val {
|
|
return false
|
|
}
|
|
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
|
|
func invertTree(root *TreeNode) *TreeNode {
|
|
if root == nil {
|
|
return nil
|
|
}
|
|
invertTree(root.Left)
|
|
invertTree(root.Right)
|
|
root.Left, root.Right = root.Right, root.Left
|
|
return root
|
|
}
|