# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
def countNodes(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root:
return 0
stack = list()
count = 1
stack.append(root)
while stack:
tmp = stack.pop(0)
if tmp.left:
count += 1
stack.append(tmp.left)
if tmp.right:
count += 1
stack.append(tmp.right)
return count
来源:CSDN
作者:宋建国
链接:https://blog.csdn.net/hot7732788/article/details/104646733