513.找树左下角的值
给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。
# 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
from collections import deque
import math
class Solution:
def findBottomLeftValue(self, root: Optional[TreeNode]) -> int:
# 递归
self.res = None
self.dept_ = -float('inf')
self.getLeftValue(root, 0)
return self.res
# 非递归
# value = root.val
# q_ = deque()
# q_.append(root)
# while q_:
# value = q_[0].val
# for _ in range(len(q_)):
# node = q_.popleft()
# if node.left:
# q_.append(node.left)
# if node.right:
# q_.append(node.right)
# return value
def getLeftValue(self, root, dept):
if not root: return
# 最后一层一定是叶子结点
if not root.left and not root.right:
if dept > self.dept_:
# 递归遍历的顺序是先左结点再右结点,因此第一个满足条件的结点要么是第一个左结点,要么是只有右结点没有左结点
self.res = root.val
self.dept_ = dept
self.getLeftValue(root.left, dept + 1)
self.getLeftValue(root.right, dept + 1)
112.路径总和
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
# 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 hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
self.flag = 0
self.path = []
self.pathSum(root, self.path, targetSum)
return False if self.flag == 0 else True
# 回溯思想来解决
def pathSum(self, root, path, targetSum):
if not root: return
path.append(root)
if not root.left and not root.right:
if self.sumNode(path) == targetSum:
self.flag = 1
if root.left:
self.pathSum(root.left, path, targetSum)
path.pop()
if root.right:
self.pathSum(root.right, path, targetSum)
path.pop()
def sumNode(self, path):
sum_ = 0
for node in path:
sum_ += node.val
return sum_
113.路径总和 II
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
# 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 __init__(self):
self.res = []
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
self.allPath(root, [], targetSum)
return self.res
def allPath(self, root, path, targetSum):
if not root: return
path.append(root.val)
targetSum -= root.val
if not root.left and not root.right and targetSum == 0:
self.res.append(list(path))
if root.left:
self.allPath(root.left, path, targetSum)
path.pop()
if root.right:
self.allPath(root.right, path, targetSum)
path.pop()
106.从中序与后序遍历序列构造二叉树
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
# 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 buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]:
if not postorder: return None
rootVal = postorder[-1]
root = TreeNode(rootVal)
sperateIndex = inorder.index(rootVal)
inorderLeft = inorder[:sperateIndex]
inorderRight = inorder[sperateIndex+1:]
postorderLeft = postorder[:len(inorderLeft)]
postorderRight = postorder[len(inorderLeft): -1]
root.left = self.buildTree(inorderLeft, postorderLeft)
root.right = self.buildTree(inorderRight, postorderRight)
return root
105.从前序与中序遍历序列构造二叉树
给定两个整数数组 preorder
和 inorder
,其中 preorder
是二叉树的先序遍历, inorder
是同一棵树的中序遍历,请构造二叉树并返回其根节点。
# 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 buildTree(self, preorder: List[int], inorder: List[int]) -> Optional[TreeNode]:
if not preorder: return None
rootVal = preorder[0]
root = TreeNode(val=rootVal)
sperateIndex = inorder.index(rootVal)
inorderLeft = inorder[:sperateIndex]
inorderRight = inorder[sperateIndex+1:]
preorderLeft = preorder[1:len(inorderLeft)+1]
preorderRight = preorder[len(inorderLeft)+1:]
root.left = self.buildTree(preorderLeft, inorderLeft)
root.right = self.buildTree(preorderRight, inorderRight)
return root