目录
二叉树05
LeetCode513:找树左下角的值
思路:要找最后一行最左边的值,很显然用层序遍历是最简单的
//迭代法
class Solution {
public int findBottomLeftValue(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
int res = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int i = 0; i < size; i++) {
TreeNode poll = queue.poll();
if (i == 0) {
res = poll.val;
}
if (poll.left != null) {
queue.offer(poll.left);
}
if (poll.right != null) {
queue.offer(poll.right);
}
}
}
return res;
}
}
LeetCode112:路径总和
思路:很显然提到路径,又是采取回溯加递归的常见做法
在LeetCode257 二叉树的所有路径中我们已经知道怎么获得所有路径
但是怎么知道是否存在这条路径上所有节点的和=目标sum呢?
-让计数器先初始化为目标和targetsum,在回溯的过程中再依次递减,如果最终到叶子结点targetsum==0,则说明存在
首先构造回溯函数,返回值为bool,传递参数为根节点root和targetsum;
终止条件:如果到了叶子结点且targetsum==0,则返回true
单层递归的逻辑:首先《中》是否为空,是否符合终止条件;然后判断《左》《右》,如果都不符合最终return false
class solution {
public boolean haspathsum(treenode root, int targetsum) {
if (root == null) {
return false;
}
targetsum -= root.val;
// 叶子结点
if (root.left == null && root.right == null) {
return targetsum == 0;
}
if (root.left != null) {
boolean left = haspathsum(root.left, targetsum);
if (left) { // 已经找到
return true;
}
}
if (root.right != null) {
boolean right = haspathsum(root.right, targetsum);
if (right) { // 已经找到
return true;
}
}
return false;
}
}
LeetCode113:路径总和ii
思路:不同于112只需要返回true或false,
递归函数需要返回满足条件的所有路径,所以不要返回值!并且用迭代方式记录所有路径比较麻烦,也没有必要。
首先构造回溯函数:void,传递的参数为root、临时路径path和返回总路径res,以及targetsum(用于判断终止条件)
终止条件:同样是到了叶子结点且targetsum==0,则把path拷贝一份添加到res中,因为后续对路径的更改会影响已经加入结果集的路径。
单层递归的逻辑:首先《中》是否为空,是否符合终止条件;然后判断《左》《右》,然后最终return res
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> path = new LinkedList<>();
if(root == null) return res;
traversal(root,path,res,targetSum);
return res;
}
public void traversal(TreeNode root,List<Integer> path,List<List<Integer>> res,int targetSum){
path.add(root.val);
targetSum -= root.val;
if(root.left==null && root.right==null && targetSum==0){
res.add(new LinkedList<>(path));
}
if(root.left!=null){
traversal(root.left,path,res,targetSum);
path.remove(path.size()-1); //回溯
}
if(root.right!=null){
traversal(root.right,path,res,targetSum);
path.remove(path.size()-1); //回溯
}
}
}
LeetCode437 路径总和iii
思路 :双重递归,第一个递归里求的是不用包含根节点和叶子结点的目标路径和个数
第二个递归是以root为根节点的符合条件的路径和个数
没问具体的路径就不用回溯
class Solution {
public int pathSum(TreeNode root, int targetSum) {
if(root == null) return 0;
int sum = rootSum(root,targetSum); //以当前节点为根节点的路径和
sum += pathSum(root.left,targetSum); //树中的路径和(不一定要包含根节点和和叶子结点)
sum += pathSum(root.right,targetSum);
return sum;
}
public int rootSum(TreeNode root, int targetSum){
int sum = 0;
if(root == null) return 0;
targetSum -= root.val;
if(targetSum == 0){
sum++;
}
if(root.left != null){
sum += rootSum(root.left,targetSum);
}
if(root.right != null){
sum += rootSum(root.right,targetSum);
}
return sum;
}
}
LeetCode124 二叉树最大路径和
思路:通过递归函数计算节点最大贡献值
class Solution {
int maxd = Integer.MIN_VALUE;
public int maxPathSum(TreeNode root) {
maxGain(root);
return maxd;
}
public int maxGain(TreeNode root){
if(root == null) return 0;
//节点最大贡献值大于0时才会添加
int left = Math.max(maxGain(root.left),0);
int right = Math.max(maxGain(root.right),0);
//计算路径和,得到最大路径和
int pathSum = left + right + root.val;
maxd = Math.max(maxd,pathSum);
return root.val + Math.max(left,right); //选择贡献值较大的一边路径
}
}
LeetCode106: 从中序与后序遍历序列构造二叉树
首先看切割逻辑:(用map保存分割inorder的节点序号非常便于查找)
来看一下一共分几步:
-
第一步:如果数组大小为零的话,说明是空节点了。
-
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
-
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
-
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
-
第五步:切割后序数组,切成后序左数组和后序右数组
-
第六步:递归处理左区间和右区间
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
root.left = findNode(inorder, inBegin, rootIndex,
postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder, rootIndex + 1, inEnd,
postorder, postBegin + lenOfLeft, postEnd - 1);
思路:区间通过左开右闭来处理
总体代码:
class Solution {
Map<Integer, Integer> map; // 方便根据数值查找位置
public TreeNode buildTree(int[] inorder, int[] postorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}
return findNode(inorder, 0, inorder.length, postorder,0, postorder.length); // 前闭后开
}
public TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
// 参数里的范围都是前闭后开
if (inBegin >= inEnd || postBegin >= postEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(postorder[postEnd - 1]); // 找到后序遍历的最后一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定后序数列的个数
root.left = findNode(inorder, inBegin, rootIndex,
postorder, postBegin, postBegin + lenOfLeft);
root.right = findNode(inorder, rootIndex + 1, inEnd,
postorder, postBegin + lenOfLeft, postEnd - 1);
return root;
}
}
LeetCode105:从前序和中序遍历序列构造二叉树
同理
class Solution {
Map<Integer, Integer> map;
public TreeNode buildTree(int[] preorder, int[] inorder) {
map = new HashMap<>();
for (int i = 0; i < inorder.length; i++) { // 用map保存中序序列的数值对应位置
map.put(inorder[i], i);
}
return findNode(preorder, 0, preorder.length, inorder, 0, inorder.length); // 前闭后开
}
public TreeNode findNode(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
// 参数里的范围都是前闭后开
if (preBegin >= preEnd || inBegin >= inEnd) { // 不满足左闭右开,说明没有元素,返回空树
return null;
}
int rootIndex = map.get(preorder[preBegin]); // 找到前序遍历的第一个元素在中序遍历中的位置
TreeNode root = new TreeNode(inorder[rootIndex]); // 构造结点
int lenOfLeft = rootIndex - inBegin; // 保存中序左子树个数,用来确定前序数列的个数
root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
inorder, inBegin, rootIndex);
root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
inorder, rootIndex + 1, inEnd);
return root;
}
}