周末太忙了,终于把后两题另一种写法补齐了。
110.平衡二叉树
- 题目链接: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.二叉树的所有路径
- 题目链接:257.二叉树的所有路径
- 回溯,注意path.pop() pop的是cur.left
# 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