第十七天 110. 平衡二叉树 257. 二叉树的所有路径 404. 左叶子之和

110. 平衡二叉树

要关注的一个点是:

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。

因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中)

题目:给定一个二叉树,判断它是否是 平衡二叉树 (平衡二叉树 是指该树所有节点的左右子树的深度相差不超过 1。)

#两种递归法
class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        return self.get_depth(root) != -1
    def get_depth(self,node):
        if not node:
            return 0
        leftdepth=self.get_depth(node.left)
        rightdepth=self.get_depth(node.right)
        if leftdepth==-1 or rightdepth==-1 or abs(leftdepth-rightdepth)>1:
            return -1
        else:
            return 1+max(leftdepth,rightdepth)



class Solution:
    def isBalanced(self, root: Optional[TreeNode]) -> bool:
        def get_h(node):
            if node is None:
                return 0
            left_h=get_h(node.left)
            if left_h==-1:
                return -1
            right_h=get_h(node.right)
            if right_h==-1 or abs(left_h-right_h)>1:
                return -1
            return 1+max(left_h,right_h)
        return get_h(root)!=-1

  257. 二叉树的所有路径

本题会用到前序遍历和回溯

给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。

叶子节点 是指没有子节点的节点。

 重点掌握递归法

class Solution:
    def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
        path=''
        res=[]
        if not root:
            return res
        self.traversal(root,path,res)
        return res
    def traversal(self,node,path,res):
        path+=str(node.val)
        if not node.left and not node.right:
            res.append(path)
        if node.left:
            self.traversal(node.left,path+'->',res)
        if node.right:
            self.traversal(node.right,path+'->',res)



class Solution:
    def traversal(self,node,path,result):
        path.append(node.val)
        if node.left is None and node.right is None:
            sPath = '->'.join(map(str, path))
            result.append(sPath)
            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]:
        result=[]
        path=[]
        if not root:
            return result
        self.traversal(root,path,result)
        return result

404. 左叶子之和

判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子。

给定二叉树的根节点 root ,返回所有左叶子之和。

class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        if  not root.left and not root.right:
            return 0
        Leftvalue=self.sumOfLeftLeaves(root.left)
        if root.left and not root.left.left and not root.left.right:
            Leftvalue=root.left.val
        Rightvalue=self.sumOfLeftLeaves(root.right)
        sum_val=Leftvalue+Rightvalue
        return sum_val



#还有迭代法
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        if not root:
            return 0
        stack=[root]
        res=0
        while stack:
            node=stack.pop()
            if node.left and not node.left.left and not node.left.right:
                res+=node.left.val
            if node.left:
                stack.append(node.left)
            if node.right:
                stack.append(node.right)
        return res

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值