LeetCode热题100道-Day07
func isSymmetric(root *TreeNode) bool {
return m1(root.Left, root.Right)
}
func m1(l,r *TreeNode) bool{
if l==nil || r==nil {
return l==r
}
if l.Val!=r.Val {
return false
}
return m1(l.Left, r.Right) && m1(l.Right, r.Left)
}
- 遍历数组,用一个bool类型的切片记录出现过的数字。随后遍历该切片,找到没有出现的数字
func findDisappearedNumbers(nums []int) []int {
X := make([]bool, len(nums)+1)
for _,v := range nums {
X[v] = true
}
res := make([]int,0)
for i:=1; i<=len(nums); i++ {
if !X[i] {
res = append(res, i)
}
}
return res
}
func hammingDistance(x int, y int) int {
i := x ^ y
res := 0
for i != 0 {
if (i & 1) == 1 {
res++
}
i = i >> 1
}
return res
}
var res int
func diameterOfBinaryTree(root *TreeNode) int {
res = 0
tra(root)
return res
}
func tra(root *TreeNode) int {
if root == nil {
return 0
}
l := tra(root.Left)
r := tra(root.Right)
sum := l + r
res = maxFunc(sum, res)
return maxFunc(l, r) + 1
}
func maxFunc(x, y int) int {
if x > y {
return x
}
return y
}