python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def countNodes(self, root: TreeNode):
left, right = root, root
l, r = 0, 0
while left:
left = left.left
l += 1
while right:
right = right.right
r += 1
# 满二叉树
if l == r:
return pow(2, l) - 1
# 非满二叉树
return 1 + self.countNodes(root.left) + self.countNodes(root.right)