代码随想录算法训练营DAY17|110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和

周末太忙了,终于把后两题另一种写法补齐了。

110.平衡二叉树

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def countheight(self, root):
        if not root:
            return 0
        
        if not root.left and not root.right:
            return 1
        
        lh=self.countheight(root.left)
        rh=self.countheight(root.right)
        
        if lh<rh-1 or rh<lh-1 or rh==-1 or lh==-1:
            return -1
        else:
            return max(lh,rh)+1
        
    def isBalanced(self, root):
        """
        :type root: TreeNode
        :rtype: bool
        """
        
        height=self.countheight(root)
        
        return False if height<0 else True

257.二叉树的所有路径

 # Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def traversal(self, cur, path, result):
        path.append(cur.val)
        if not cur.left and not cur.right: #叶子结点
            str_ = []
            for node in path:
                str_.append(str(node))
            result.append('->'.join(str_))
        
        if cur.left:
            self.traversal(cur.left, path, result)
            path.pop()
        if cur.right:
            self.traversal(cur.right, path, result)
            path.pop()
        
        return result
    def binaryTreePaths(self, root):
        """
        :type root: TreeNode
        :rtype: List[str]
        """
        result = []
        path = collections.deque()
        
        if not root:
            return result

        return self.traversal(root, path, result)
  • 迭代,注意每次的path修改不能加在原path上
 # Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def binaryTreePaths(self, root):
        """
        :type root: TreeNode
        :rtype: List[str]
        """
        st = collections.deque([root])
        path_total = [[str(root.val)]]
        result = []
        
        if not root:
            return result

        while st:
            cur = st.pop()
            path = path_total.pop()
            if not cur.left and not cur.right:
                result.append('->'.join(path))
            if cur.right:
                st.append(cur.right)
                path_total.append(path+[str(cur.right.val)])
            if cur.left:
                st.append(cur.left)
                path_total.append(path+[str(cur.left.val)])
        return result

404.左叶子之和

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root:
            return 0
        
        if not root.left and not root.right:
            return 0
        
        ls=self.sumOfLeftLeaves(root.left)
        rs=self.sumOfLeftLeaves(root.right)
        
        if not root.left:
            return rs
        
        if not root.left.left and not root.left.right:
            return ls+rs+root.left.val
        else:
            return ls+rs
  • 迭代(前序遍历)
# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def sumOfLeftLeaves(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        st = collections.deque([root])
        result = 0

        while st:
            cur = st.pop()
            if cur.right:
                st.append(cur.right)
            if cur.left:
                st.append(cur.left)
                if not cur.left.left and not cur.left.right:
                    result+=cur.left.val     
                    st.pop()

        return result
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值