1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
class (object): def countNodes(self, root): """ :type root: TreeNode :rtype: int """ if not root: return 0 left = right = root l = r = 0 while left: l += 1 left = left.left while right: r += 1 right = right.right if r == l: return 2**r -1 else: return self.countNodes(root.left) + self.countNodes(root.right) + 1
|
近期评论