110. Balanced Binary Tree 257. Binary Tree Paths 404. Sum of Left Leaves

Given a binary tree, determine if it is height-balanced

一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1

The absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

why postorder  traversal?  left-right-node

Because once one of the left and right subtrees is not a balanced tree, the whole tree is not a balanced tree

 iteration:

# 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:
        if self.get_height(root) != -1:
            return True
        else:
            return False

    def get_height(self, root):
        if not root:
            return 0
        left_height = self.get_height(root.left) #左
        right_height = self.get_height(root.right) #右

        if left_height == -1 or right_height == -1 or abs(left_height - right_height) > 1:
            return -1
        return 1 + max(left_height, right_height)  #中

 

257. Binary Tree Paths

Given the root of a binary tree, return all root-to-leaf paths in any order.

leaf is a node with no children.

 

preorder traversal + iteration + backtracking:

如果 path[1, 2, 3],那么 map(str, path) 将会得到一个迭代器 ['1', '2', '3'],然后 join 方法将它们连接成 '1->2->3'。 

# 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
# Definition for a binary tree node.
class Solution:
    def traversal(self, cur, path, result):
        path.append(cur.val)  # 中
        if not cur.left and not cur.right:  # 到达叶子节点
            sPath = '->'.join(map(str, path))
            result.append(sPath)
            return
        if cur.left:  # 左
            self.traversal(cur.left, path, result)
            path.pop()  # 回溯
        if cur.right:  # 右
            self.traversal(cur.right, path, result)
            path.pop()  # 回溯

    def binaryTreePaths(self, root):
        result = []
        path = []
        if not root:
            return result
        self.traversal(root, path, result)
        return result

404. Sum of Left Leaves

Given the root of a binary tree, return the sum of all left leaves.

leaf is a node with no children. A left leaf is a leaf that is the left child of another node.

postorder traversal + iteration:

 

myown way:

s 是一个整数,属于不可变对象。所以在递归过程中对 s 的修改实际上会创建一个新的对象,但不会影响原始的 s

# 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:
        if not root:
            return 0
        s = [0] # 不能s = 0 ,因为 s为[]时候才能在iteration中被修改
        self.leftsum(s, root)
        return s[0]
    
    def leftsum(self, s, node):
        if node.left:
            self.leftsum(s, node.left)
        if node.right:
            self.leftsum(s, node.right)
        if node.left and not node.left.left and not node.left.right:
            s[0] += node.left.val

simple way:

class Solution:
    def sumOfLeftLeaves(self, root):
        if root is None:
            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)

recursion way:

# 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:
        if not root:
            return 0
        stack = [root]
        result = 0

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值