/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcisBalanced(root *TreeNode)bool{
res :=getHight(root)if res ==-1{returnfalse}returntrue}funcgetHight(root *TreeNode)int{if root ==nil{return0}
l , r :=getHight(root.Left),getHight(root.Right)if(l ==-1|| r ==-1){return-1}if(l - r >1|| r - l >1){return-1}returnmax(l , r)+1}funcmax(a int, b int)int{if a > b{return a
}return b
}
257. 二叉树的所有路径
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcbinaryTreePaths(root *TreeNode)[]string{var res []stringif root ==nil{return res
}var backAl func(root *TreeNode , s string)
backAl =func(root *TreeNode , s string){if root.Right ==nil&& root.Left ==nil{
a := s + strconv.Itoa(root.Val)
res =append(res , a)return}
s = s + strconv.Itoa(root.Val)+"->"if root.Left !=nil{backAl(root.Left , s)}if root.Right !=nil{backAl(root.Right , s)}}backAl(root,"")return res
}
404.左叶子之和
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcsumOfLeftLeaves(root *TreeNode)int{
sum :=0if root ==nil{return sum
}var backAl func(root *TreeNode)
backAl =func(root *TreeNode){if(root.Left ==nil&& root.Right ==nil){return}if(root.Left !=nil&& root.Left.Left ==nil&& root.Left.Right ==nil){
sum = sum + root.Left.Val
}if root.Left !=nil{backAl(root.Left)}if root.Right !=nil{backAl(root.Right)}}backAl(root)return sum
}