📢博客主页:卷测开的快乐人 📢欢迎点赞 👍 收藏 ⭐留言 📝 欢迎讨论!
📢本文由 【卷测开的快乐人】 原创!首发于 CSDN🙉🙉🙉 📢
由于博主是在学小白一枚,难免会有错误,有任何问题欢迎评论区留言指出,感激不 尽!✨ 📖精品专栏(不定时更新)✨
带值的递归模板
如果遇到题传出是int可以使用这个,完了我会出一个专辑,把所有模板总结起来,大家关注一下,感谢支持!
public int sumOfLeftLeaves(TreeNode root){
if(root ==null){
return 0;
}
int leftSum = sumOfLeftLeaves(root.left);
int rightSum = sumOfLeftLeaves(root.right);
return sum ;
}
力扣404 左子树之和
链接:https://leetcode-cn.com/problems/sum-of-left-leaves/
给定二叉树的根节点 root ,返回所有左叶子之和。
输入: root = [3,9,20,null,null,15,7]
输出: 24
解释: 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24
思路
- 首先第一个结点肯定不算进去,因为要求只是左结点
- 怎么可以得到左结点,可以用层序遍历去拿到第一个左边的结点,但是想到这里就理解错题题意了,因为这个是所有左节点,并不是最左的结点。
- 我的思路是遍历整个二叉树,判断左边的树是否为空,不为空就加
- 那么判断当前节点是不是左叶子是无法判断的,必须要通过节点的父节点来判断其左孩子是不是左叶子,并且这里的子节点,是独立的子结点
- 我们用如下代码进行判断
if(node.ledt==null&&node.left.left==null&&node.left.right==null)
递归实现
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if(root ==null){
return 0;
}
int leftSum = sumOfLeftLeaves(root.left);
int rightSum = sumOfLeftLeaves(root.right);
int tmp =0 ;
if(root.left!=null&&root.left.left==null&&root.left.right==null){
tmp = root.left.val;
}
int sum = tmp + leftSum + rightSum;
return sum ;
}
}
知识点:如果需要遍历整棵树,递归函数就不能有返回值。如果需要遍历某一条固定路线,递归函数就一定要有返回值!
513.找树左下角的值
上道题是典型的遍历值的,而这道题是只需要得到一个值的。
链接:https://leetcode-cn.com/problems/find-bottom-left-tree-value/
给定一个二叉树,在树的最后一行找到最左边的值。
假设二叉树中至少有一个节点。
示例 1:
思路
- 可以根据层序遍历选取最左边的也就是每一层的第一个数,该题只要求最左边的值。
- 分解步骤,1.层序遍历,2.记录当前的层数(获取最大层数) 3.输出左值
- 本题还需要类里的两个全局变量,maxLen用来记录最大深度,maxleftValue记录最大深度最左节点的数值。
- 那么如果找最左边的呢?可以使用前序遍历,这样才先优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
- (前序遍历怎么命名呢,和根的遍历位置有关)
遍历实现
一个结点:如果只有一个结点,就直接返回value,因为到了findLeftValue()中判断为空会跳过。
多个结点:如果多个结点,会根据前序遍历左边的结点是否为空。(前序遍历还是非常重要的,二叉树力扣题,无非就这些变换)
value = root.val;
findLeftValue(root,0);
return value;
class Solution {
private int Deep = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root,0);
return value;
}
public void findLeftValue(TreeNode root,int deep){
if(root==null){
return ;
}
if(root.left == null && root.right == null){
if (deep > Deep) {
value = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left,deep + 1);
if (root.right != null) findLeftValue(root.right,deep + 1);
}
}
112. 路径总和
给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。
叶子节点 是指没有子节点的节点。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/path-sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
输入:root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
输出:true
解释:等于目标和的根节点到叶节点路径如上图所示。
思路:
用到前序遍历思路,每深入一次,sum-根节点的值,当到达叶子节点的时候,判断sum是否等于当前的节点值,如果等于,说明找到了,直接退出循环,否则尝试另外一条路径。
代码如下:
class Solution {
boolean ans =false;
public void dfs(TreeNode root,int sum ){
if(root==null){
return ;
}
if(root.left == null&& root.right ==null){
if(sum == root.val){
ans = true;
return ;
}
}
dfs(root.left,sum - root.val);
dfs(root.right,sum - root.val);
}
public boolean hasPathSum(TreeNode root, int targetSum) {
if(root == null){
return false;
}
dfs(root,sum);
return ans;
}
}
106.从中序与后序遍历序列构造二叉树
如果是二叉搜索树,只需要前序或者后续,如果是普通二叉树,需要前序和中序,或者中序和后续,因为需要区分左右子树。
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
知道后序遍历之后可以去切割法,对中序遍历进行切割,
因为后序遍历的最后一个元素肯定是中间节点。
比如 后续 9 15 7 20 3我们拿3对中序进行分割
得到 9 和15 20 17 然后依次遍历即可。
具体代码:
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
return buildTree1(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
public TreeNode buildTree1(int[] inorder, int inLeft, int inRight,
int[] postorder, int postLeft, int postRight) {
// 没有元素了
if (inRight - inLeft < 1) {
return null;
}
// 只有一个元素了
if (inRight - inLeft == 1) {
return new TreeNode(inorder[inLeft]);
}
// 后序数组postorder里最后一个即为根结点
int rootVal = postorder[postRight - 1];
TreeNode root = new TreeNode(rootVal);
int rootIndex = 0;
// 根据根结点的值找到该值在中序数组inorder里的位置
for (int i = inLeft; i < inRight; i++) {
if (inorder[i] == rootVal) {
rootIndex = i;
break;
}
}
// 根据rootIndex划分左右子树
root.left = buildTree1(inorder, inLeft, rootIndex,
postorder, postLeft, postLeft + (rootIndex - inLeft));
root.right = buildTree1(inorder, rootIndex + 1, inRight,
postorder, postLeft + (rootIndex - inLeft), postRight - 1);
return root;
}
}
同理可得前序和中序确定一个二叉树。
class Solution {
public TreeNode buildTree(int[] preorder, int[] inorder) {
return helper(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1);
}
public TreeNode helper(int[] preorder, int preLeft, int preRight,
int[] inorder, int inLeft, int inRight) {
// 递归终止条件
if (inLeft > inRight || preLeft > preRight) return null;
// val 为前序遍历第一个的值,也即是根节点的值
// idx 为根据根节点的值来找中序遍历的下标
int idx = inLeft, val = preorder[preLeft];
TreeNode root = new TreeNode(val);
for (int i = inLeft; i <= inRight; i++) {
if (inorder[i] == val) {
idx = i;
break;
}
}
// 根据 idx 来递归找左右子树
root.left = helper(preorder, preLeft + 1, preLeft + (idx - inLeft),
inorder, inLeft, idx - 1);
root.right = helper(preorder, preLeft + (idx - inLeft) + 1, preRight,
inorder, idx + 1, inRight);
return root;
}
}