树-222. 完全二叉树的节点个数- PYTHON

点点圈 提交于 2020-03-04 09:46:00

在这里插入图片描述

# 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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!