/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcmaxDepth(root *TreeNode)int{var seekDepth func(root *TreeNode,depth int)if root ==nil{return0}
res :=0//queue := list.New()
seekDepth =func(root *TreeNode,depth int){if root ==nil{return}if(res < depth){
res = depth
}seekDepth(root.Left,depth+1)seekDepth(root.Right,depth+1)}seekDepth(root,0)return res +1}
559.n叉树的最大深度
/**
* Definition for a Node.
* type Node struct {
* Val int
* Children []*Node
* }
*/funcmaxDepth(root *Node)int{
count :=0if root ==nil{return count
}
ss := list.New()
ss.PushBack(root)for ss.Len()>0{
length := ss.Len()
count++for i :=0; i < length ; i++{
node := ss.Remove(ss.Front()).(*Node)for j :=0; j <len(node.Children); j++{
ss.PushBack(node.Children[j])}}}return count
}
● 111.二叉树的最小深度
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funcminDepth(root *TreeNode)int{
queue := list.New()if root ==nil{return0}
queue.PushBack(root)
depth :=0for queue.Len()>0{
length := queue.Len()
depth = depth +1for i :=0; i < length ; i++{
node := queue.Remove(queue.Front()).(*TreeNode)if(node.Left ==nil&& node.Right ==nil){return depth
}if(node.Left !=nil){
queue.PushBack(node.Left)}if(node.Right !=nil){
queue.PushBack(node.Right)}}}
222.完全二叉树的节点个数
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/funccountNodes(root *TreeNode)int{
ss := list.New()
count :=0if root ==nil{return count
}
ss.PushBack(root)for ss.Len()>0{
length := ss.Len()for i :=0; i < length ; i++{
node := ss.Remove(ss.Front()).(*TreeNode)
count++if(node.Left !=nil){
ss.PushBack(node.Left)}if(node.Right !=nil){
ss.PushBack(node.Right)}}}return count
}