• 222.Count Complete Tree Nodes


    Given a complete binary tree, count the number of nodes.

    Note:

    Definition of a complete binary tree from Wikipedia:
    In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

    Example:

    Input:

        1
       / 
      2   3
     /   /
    4  5 6
    

    Output: 6

    Solution1:(TLE)

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def countNodes(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root is None:
                return 0
            return 1+self.countNodes(root.left)+self.countNodes(root.right)
    

    Solution2:(TLE) why!

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def countNodes(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if root is None:
                return 0
            def leftheight(root):
                count = 0
                while root:
                    count += 1
                    root = root.left
                return count
            def rightheight(root):
                count = 0
                while root:
                    count += 1
                    root = root.right
            l = leftheight(root)
            r = rightheight(root)
            if l==r:
                return 2**l-1
            return 1 + self.countNodes(root.left) + self.countNodes(root.right)
    

    Solution3:

    # Definition for a binary tree node.
    class TreeNode:
        def __init__(self, x):
            self.val = x
            self.left = None
            self.right = None
    
    class Solution:
        def countNodes(self, root):
            """
            :type root: TreeNode
            :rtype: int
            """
            if self.countLeft(root) == self.countRight(root):
                return (1<<self.countLeft(root)) - 1
            else:
                return self.countNodes(root.left) + self.countNodes(root.right) + 1
    
        def countLeft(self, root):
            ans = 0
            while root:
                ans += 1
                root = root.left
            return ans
    
        def countRight(self, root):
            ans = 0
            while root:
                ans += 1
                root = root.right
            return ans
    
  • 相关阅读:
    后台点赞 接口
    三表联查
    后台投票 接口
    MSXML insertBefore(IXMLDOMNode *newChild, VARIANT refChild) 传参
    WTL中菜单栏及工具栏项状态改变应注意的地方
    使用WTL的消息反射封装CEdit实现监听控件文本改变事件
    修改字体
    CEdit实现文本换行
    VC中获取窗口控件相对客户区的坐标
    关闭HTC手机充电时屏幕一直亮着绿色电池的办法
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/9774147.html
Copyright © 2020-2023  润新知