[leetcode]大可日常打卡-树

本文深入探讨了二叉树与链表的相关算法,包括最长同值路径、翻转二叉树、路径总和等问题,并给出了具体的实现代码。

687最长同值路径

    一开始非常没有思路。看了解析以后,找到了思路,把长度看作是从一个节点向左右子树延伸出去的两个箭头,从下往上遍历,后序遍历递归,如果箭头上的值等于节点的值,那么那么路径的长度就加1。然后刚开始写出的代码分别计算两边的,然后加起来。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int max=0;
    public int longestUnivaluePath(TreeNode root) {
        help(root);
        return max;
    }
    public int help(TreeNode root){
        if(root==null){
            return 0;
        }
        int left=help(root.left);
        int right=help(root.right);
        if(root!=null&&root.left!=null){
            if(root.val==root.left.val){
                left++;
            }else{
                left=0;
            }
        }
        if(root!=null&&root.right!=null){
            if(root.val==root.right.val){
                right++;
            }else{
                right=0;
            }
        }
        max=Math.max(max,left+right);
        return Math.max(left,right);
    }
}

226翻转二叉树

    递归。交换二叉树左右子树。然后对每一个左右子树进行递归。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return root;
        }
        TreeNode temp=root.left;
        root.left=root.right;
        root.right=temp;
        if(root.left!=null){
            invertTree(root.left);
        }
        if(root.right!=null){
            invertTree(root.right);
        }
        return root;
    }
}

112路径总和

    给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。

说明: 叶子节点是指没有子节点的节点。

思路:参考剑指offer34,二叉树中和为某一值的路径。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> path=new ArrayList<Integer>(); //每次都新建一个,题目没有让找出具体路径,只要有就返回true,找不到就返回false
    public boolean hasPathSum(TreeNode root, int sum) {
        findPath(root,sum);
        if(path.size()==0){
            return false;
        }
        return true;
    }
    public ArrayList<Integer> findPath(TreeNode root,int sum){
        if(root==null){
            return path;
        }
        // if((sum>0&&root.val>sum)||(sum<0&&root.val<sum)){
        //     return path;
        // }
        path.add(root.val);
        sum-=root.val;
        if(sum==0&&root.left==null&&root.right==null){
            return path; //找到路径啦!
        }
        findPath(root.left,sum);
        findPath(root.right,sum);
        path.remove(path.size()-1);
        return path;
    }
        
}

113路径总和 II 

    给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    List<List<Integer>> pathlist=new ArrayList();
    List<Integer> path=new ArrayList();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if(root==null){
            return pathlist;
        }
        path.add(root.val);
        sum-=sum=root.val;
        if(sum==0&&root.left==null&&root.right==null){
            pathlist.add(new ArrayList<>(path));
        }
        pathSum(root.left,sum);
        pathSum(root.right,sum);
        path.remove(path.size()-1);
        return pathlist;
    }
}

437路径总和 III

    给定一个二叉树,它的每个结点都存放着一个整数值。

找出路径和等于给定数值的路径总数。

路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点到子节点)。

二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int total=0;
	ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> path=new ArrayList<Integer>();
	public int pathSum(TreeNode root, int sum) {
//		int total;
        if(root==null){
             return 0;
        }
        total=numOfPath(root,sum);
        //System.out.println(total+" "+root.val);
        pathSum(root.left,sum);
        //System.out.println(pathSum(root.left,sum)+"**");
        pathSum(root.right,sum);
        //System.out.println(pathSum(root.right,sum)+"**");
        return total;
    }
    
    public int numOfPath(TreeNode root,int sum){ //对于树root的每一个节点来说,返回以这个节点为根节点的和为sum的路径个数
        if(root==null){
            return pathlist.size();
        }
        path.add(root.val);
        sum-=root.val;
        if(sum==0){
        	
        	for(int i:path){
        	//	System.out.print(i+" ");
        	}
        	//System.out.println();
        	
        	pathlist.add(new ArrayList<>(path));
        
        }
        if(root.left!=null){
        	numOfPath(root.left,sum);
        }
        if(root.right!=null){
        	numOfPath(root.right,sum);
        }
        
        path.remove(path.size()-1);
        
        //System.out.println(pathlist.size());
        return pathlist.size();
    }
}

    未通过!

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    int total=0;
	ArrayList<ArrayList<Integer>> pathlist=new ArrayList<ArrayList<Integer>>();
    ArrayList<Integer> path=new ArrayList<Integer>();
	public int pathSum(TreeNode root, int sum) {
//		int total;
        if(root==null){
             return total;
        }
        total=numOfPath(root,sum);
        //System.out.println(total+" "+root.val);
        pathSum(root.left,sum);
        //System.out.println(pathSum(root.left,sum)+"**");
        pathSum(root.right,sum);
        //System.out.println(pathSum(root.right,sum)+"**");
        return total;
    }
    
    public int numOfPath(TreeNode root,int sum){ //对于树root的每一个节点来说,返回以这个节点为根节点的和为sum的路径个数
        if(root==null){
            return pathlist.size();
        }
        path.add(root.val);
        sum-=root.val;
        if(sum==0){
        	//for(int i:path){
        	//	System.out.print(i+" ");
        	//}
        	//System.out.println();
        	pathlist.add(path);
            //return pathlist.size();
        }
        if(root.left!=null){
        	numOfPath(root.left,sum);
            path.remove(path.size()-1);
        }
        if(root.right!=null){
        	numOfPath(root.right,sum);
            path.remove(path.size()-1);
        }
        
        return pathlist.size();
    }
}

109有序链表转换二叉搜索树

思路:先根据给的例子寻找思路!根据题意得,要求出得是平衡二叉搜索树,平衡二叉搜索树的条件是左右子树的高度差的绝对值不超过1!!所以对于单链表,我们从中间开始,找到mid位置!!先把左边变成一棵平衡二叉树返回left节点,再把右边变成一棵平衡二叉树,返回right节点,再和根连起来。

    首先,寻找单链表的中间位置,设定两个指针,一个快指针,一个慢指针,当快指针的next为null时,慢指针指向中间位置。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        if(head==null){
            return null;
        }
        if(head.next==null){
            return new TreeNode(head.val);
        }
        if(head.next.next==null){
            TreeNode root=new TreeNode(head.val);
            root.right=new TreeNode(head.next.val);
            return root;
        }
        //先找到list的中间位置
        ListNode fast=head;
        ListNode slow=head;
        while(fast.next!=null&&fast.next.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        //现在slow就是中间位置,slow前面的是left,slow后面的是right
        ListNode tmp=head;
        while(tmp.next!=slow){
            tmp=tmp.next;
        }
        tmp.next=null;
        ListNode left=head;
        ListNode right=slow.next;
        slow.next=null;//把链表从中间截断!!!!现在链表变成了left和right
        
        TreeNode root=new TreeNode(slow.val);
        TreeNode leftNode=sortedListToBST(left);
        TreeNode rightNode=sortedListToBST(right);
        if(leftNode!=null){
            root.left=leftNode;
        }
        if(rightNode!=null){
            root.right=rightNode;
        }
        
        return root;
    }
}

111二叉树的最小深度

    给定一个二叉树,找出其最小深度。

    最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

思路:参考剑指offer55,二叉树的深度,那个是求最大的,这个是求最小的。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        int min=0;
        if(root==null){
            return 0;
        }
        int left=0;
        if(root.left!=null){
            left=minDepth(root.left);
        }
        int right=0;
        if(root.right!=null){
            right=minDepth(root.right);
        }
        min=Math.min(left+1,right+1);
        return min;
    }
}

    上面的程序通不过1,2,null,null,null这个样例,应该返回2,但是它返回1。因为要找的路径应该是从根节点到叶子节点,叶子节点应该是既没有左孩子也没有右孩子的节点。但是根节点很特别。其实就是如果左右孩子里面有一个是空的话,那么min就是非空的那边的值!!

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        int min=0;
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null){
            return 1;
        }
        if(root.left==null){
            return minDepth(root.right)+1;
        }
        if(root.right==null){
            return minDepth(root.left)+1;
        }
        
        min=Math.min(minDepth(root.left)+1,minDepth(root.right)+1);
        return min;
    }
}

【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储学数据】(Matlab代码实现)内容概要:本文提出了一种基于融合鱼鹰和柯西变异的麻雀优化算法(OCSSA)优化变分模态分解(VMD)参数,并结合卷积神经网络(CNN)与双向长短期记忆网络(BiLSTM)的轴承故障诊断模型。该方法利用西储学公开的轴承数据集进行验证,通过OCSSA算法优化VMD的分解层数K和惩罚因子α,有效提升信号分解精度,抑制模态混叠;随后利用CNN提取故障特征的空间信息,BiLSTM捕捉时间序列的动态特征,最终实现高精度的轴承故障分类。整个诊断流程充分结合了信号预处理、智能优化与深度学习的优势,显著提升了复杂工况下轴承故障诊断的准确性与鲁棒性。; 适合人群:具备一定信号处理、机器学习及MATLAB编程基础的研究生、科研人员及从事工业设备故障诊断的工程技术人员。; 使用场景及目标:①应用于旋转机械设备的智能运维与故障预警系统;②为轴承等关键部件的早期故障识别提供高精度诊断方案;③推动智能优化算法与深度学习在工业信号处理领域的融合研究。; 阅读建议:建议读者结合MATLAB代码实现,深入理解OCSSA优化机制、VMD参数选择策略以及CNN-BiLSTM网络结构的设计逻辑,通过复现实验掌握完整诊断流程,并可进一步尝试迁移至其他设备的故障诊断任务中进行验证与优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值