Count Complete Tree Nodes LeetCode solution in Python
1class Solution:2# @param {TreeNode} root3# @return {integer}4def countNodes(self, root):5if not root:6return 07leftDepth = self.getDepth(root.left)8rightDepth = self.getDepth(root.right)9if leftDepth == rightDepth:10return pow(2, leftDepth) + self.countNodes(root.right)11else:12return pow(2, rightDepth) + self.countNodes(root.left)1314def getDepth(self, root):15if not root:16return 017return 1 + self.getDepth(root.left)