给定一棵完全二叉树的头节点head,返回这棵树的节点个数。
完全二叉树指:设二叉树的深度为h,则 [1,h-1] 层的节点数都满足
个
数据范围:节点数量满足
,节点上每个值都满足
进阶:空间复杂度
, 时间复杂度
package main
import . "nc_tools"
/*
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
/**
*
* @param head TreeNode类
* @return int整型
*/
func nodeNum( head *TreeNode ) int {
ans:=0
var order func(*TreeNode)
order=func(root *TreeNode){
if root==nil{
return
}
ans++
order(root.Left)
order(root.Right)
}
order(head)
return ans
}