用O(n)的时间复杂度就不说了,这里说用小于O(n)的时间复杂度。
def nums_node(root
):
if not root
:
return None
return bs
(root
,1,max_height
(root
,1))
def bs(node
,level
,height
):
if level
==height
:
return 1
if max_height
(root
.right
,depth
+1)==height
:
return (2**(height
-level
))+bs
(node
.right
, level
+1, height
)
else:
return (2**(height
-level
-1))+bs
(node
.left
, level
+1, height
)
def max_height(node
,level
):
while node
:
node
=node
.left
level
+=1
return level
-1
转载请注明原文地址:https://ipadbbs.8miu.com/read-2213.html