算法题day16(补5.6的卡)

一、刷题:

1.leetcode题目 110 平衡二叉树(easy):

先验知识:

①一颗高度平衡的二叉树定义为:一个二叉树每个节点的左右两个子树的高度差的的绝对值不超过1。

②注意深度与高度的概念:

(求深度适合用前序遍历 求高度适合用后序遍历)

解决:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        return self.get_height(root) != -1
    def get_height(self,node):
        if not node:
            return 0
        left_h = self.get_height(node.left)
        right_h = self.get_height(node.right)
        if left_h == -1 or right_h == -1 or abs(left_h - right_h) > 1:
            return -1
        return max(left_h,right_h) + 1
        

2.leetcode题目 257 二叉树的所有路径(easy):

题目描述:257. 二叉树的所有路径 - 力扣(LeetCode)

递归:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def traversal(self,node,path,result):
        path.append(node.val) #中
        if not node.left and not node.right:
            path = '->'.join(map(str,path))   ####处理方式很重要
            result.append(path)
            return
        if node.left: #左
            self.traversal(node.left,path,result)
            path.pop()
        if node.right: #右
            self.traversal(node.right,path,result)
            path.pop()

    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        path = []
        result = []
        if not root:
            return []
        self.traversal(root,path,result)
        return result

迭代:(个人觉得迭代比递归好思考)

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        stack = [root]
        path_st = [str(root.val)]
        result = []
        if not root:
            return []
        while stack:
            cur = stack.pop()
            path = path_st.pop()
            if not cur.left and not cur.right:
                result.append(path)
            if cur.left:
                stack.append(cur.left)
                path_st.append(path + '->' + str(cur.left.val))
            if cur.right:
                stack.append(cur.right)
                path_st.append(path + '->' + str(cur.right.val))
        return result

3.leetcode题目 404 左叶子之和(easy):

题目描述:404. 左叶子之和 - 力扣(LeetCode)

解决:

迭代法:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        queue = collections.deque([root])
        summ = 0
        if not root:
            return 0
        if not root.left and not root.right:
            return 0
        while queue:
            for _ in range(len(queue)):
                cur = queue.popleft()
                if cur.left and cur.left.left is None and cur.left.right is None:
                    summ += cur.left.val
                if cur.left:
                    queue.append(cur.left)
                if cur.right:
                    queue.append(cur.right)
        return summ

递归:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        summ = 0
        if not root:
            return 0
        leftvalue = 0
        if root.left is not None and root.left.left is None and root.left.right is None:
            leftvalue = root.left.val
        return leftvalue + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)

  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值