day15 第六章 二叉树part03

110.平衡二叉树 (优先掌握递归)

比较左右子树的高度->求高度用后序法

class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        if not root:
            return True
        
        height = self.getHeight(root)
        if height == -1:
            return False
        else:
            return True
  
    def getHeight(self, node: Optional[TreeNode]) -> int:
        if not node:
            return 0
        
        leftHeight = self.getHeight(node.left)
        if leftHeight == -1:
            return -1
        rightHeight = self.getHeight(node.right)
        if rightHeight == -1:
            return -1
        if abs(leftHeight - rightHeight) > 1:
            return -1
        height = 1 + max(leftHeight, rightHeight)
        return height

257. 二叉树的所有路径 (优先掌握递归)

class Solution:
    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        # 前序法
        if not root:
            return []
        
        result = []
        path = [root.val] #str(root.val) # convert it to string later
        self.getPath(root, path, result) #?
        # path_str = '->'.join(path)
        return result

    def getPath(self, node: Optional[TreeNode], path: List[int], result: List[str]) -> None:

        if not node.left and not node.right:
            path_str = '->'.join(map(str, path))
            result.append(path_str)
            return
        
        # 递归和回溯一直在一起
        if node.left:
            path.append(node.left.val)
            self.getPath(node.left, path, result)
            path.pop()
        if node.right:
            path.append(node.right.val)
            self.getPath(node.right, path, result)
            path.pop()

        

404.左叶子之和 (优先掌握递归)

class Solution:
    result = 0
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        Solution.result = 0
        self.getSum(root)
        # 前后序应该都可以吧

        return Solution.result

    def getSum(self, node: Optional[TreeNode]) -> None:
        if not node:
            return

        if node.left and not node.left.left and not node.left.right:
            Solution.result += node.left.val
            self.getSum(node.right)
            return

        self.getSum(node.left)
        self.getSum(node.right)
        return 

222.完全二叉树的节点个数(优先掌握递归)

满二叉树法:

完全二叉树最终可以分解为满二叉树

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        # 完全二叉树法
        if not root:
            return 0

        # check left depth and right depth
        leftdepth = 0
        left_node = root.left
        while left_node:
            leftdepth += 1
            left_node = left_node.left

        rightdepth = 0
        right_node = root.right
        while right_node:
            rightdepth += 1
            right_node = right_node.right

        if leftdepth == rightdepth:
            return 2**(leftdepth+1)-1
        
        return self.countNodes(root.left) + self.countNodes(root.right) + 1

        

遍历:

前序法:

class Solution:
    sum = 0
    def countNodes(self, root: Optional[TreeNode]) -> int:
        # 普通法:前序法
        Solution.sum = 0
        self.getCount(root, Solution.sum)

        return Solution.sum
            
    def getCount(self, node: Optional[TreeNode], sum: int) -> None:
        if not node:
            return
        
        Solution.sum += 1
        self.getCount(node.left, Solution.sum)
        self.getCount(node.right, Solution.sum)
        return

后序法:

class Solution:
    def countNodes(self, node: Optional[TreeNode]) -> int:
        # 普通法:后序法
        if not node:
            return 0
        leftNodes = self.countNodes(node.left)
        rightNodes = self.countNodes(node.right)
        result = 1 + leftNodes + rightNodes

        return result
            
        

层序法:

class Solution:
    def countNodes(self, root: Optional[TreeNode]) -> int:
        # 普通法:层序遍历
        result = 0
        if not root:
            return result
        
        q = collections.deque()
        q.append(root)
        while q:
            size = len(q)
            for _ in range(size):
                node = q.popleft()
                result += 1
                if node.left:
                    q.append(node.left)
                if node.right:
                    q.append(node.right)
        return result
            

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值