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) #中
Given the root
of a binary tree, return all root-to-leaf paths in any order.
A 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
Given the root
of a binary tree, return the sum of all left leaves.
A 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