当前位置: 首页 > article >正文

力扣labuladong——一刷day50

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣100. 相同的树
  • 二、力扣1367. 二叉树中的链表
  • 三、力扣105. 从前序与中序遍历序列构造二叉树
  • 四、力扣654. 最大二叉树


前言


最常见的,二叉树的列构造问题一般都会用到分解问题的思维模式。

一、力扣100. 相同的树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        if(p == null || q == null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left) && isSameTree(p.right, q.right);
    }
}

二、力扣1367. 二叉树中的链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSubPath(ListNode head, TreeNode root) {
        if(head == null){
            return true;
        }
        if(root == null){
            return false;
        }
        if(head.val == root.val){
            if(fun(root,head)){
                return true;
            }
        }
        return isSubPath(head,root.left) || isSubPath(head,root.right);
    }
    public boolean fun(TreeNode root, ListNode head){
        if(head == null){
            return true;
        }
        if(root == null){
            return false;
        }
        if(root.val != head.val){
            return false;
        }
        return fun(root.left, head.next) || fun(root.right, head.next);
    }
}

三、力扣105. 从前序与中序遍历序列构造二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return  fun(preorder,inorder, 0,preorder.length-1,0,inorder.length-1);
    }
    public TreeNode fun(int[] preorder, int[] inorder, int preLow,int preHigh, int inLow,int inHigh){
        if(preLow > preHigh){
            return null;
        }
        TreeNode cur = new TreeNode(preorder[preLow]);
        int index = 0, len = 0;
        for(int i = inLow; i <= inHigh; i ++){
            if(inorder[i] == preorder[preLow]){
                index = i;
                len = index - inLow;
                break;

            }
        }
        cur.left = fun(preorder,inorder,preLow+1,preLow+len,inLow,index-1);
        cur.right = fun(preorder,inorder,preLow+len+1,preHigh,index+1,inHigh);
        return cur;
    }
}

四、力扣654. 最大二叉树

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return fun(nums,0, nums.length-1);
    }
    public TreeNode fun(int[] nums, int low, int high){
        if(low > high){
            return null;
        }
        int index = low;
        for(int i = low; i <= high; i ++){
            index = nums[i] > nums[index] ? i:index;
        }
        TreeNode cur = new TreeNode(nums[index]);
        cur.left = fun(nums,low, index-1);
        cur.right = fun(nums,index+1,high);
        return cur;
    }
}

http://www.kler.cn/news/149555.html

相关文章:

  • Opencv 极坐标变换
  • YoloV8改进策略:基于RevCol,可逆的柱状神经网络的完美迁移,YoloV8的上分利器
  • 银河麒麟V10-ARM架构-postgresql安装与部署指南
  • 133. 面试官:JSBridge是什么?
  • Java面试题(每天10题)-------连载(39)
  • 【理解ARM架构】异常处理
  • 人工智能-优化算法之梯度下降
  • RLHF:强化学习结合大预言模型的训练方式
  • 在Mysql中,什么是回表,什么是覆盖索引,索引下推?
  • Qt 软件调试(一) Log日志调试
  • MapReduce概念
  • 简化文件上传流程:学习如何封装Vue2拖拽上传组件
  • 4.ORB-SLAM3中如何实现稠密建图(二):稠密建图如何控制三大线程与稠密建图代码解析
  • 额,收到阿里云给的赔偿了!
  • OpenCV | 傅里叶变换——低通滤波器与高通滤波器
  • 西南科技大学C++程序设计实验二(类与对象一)
  • 做到这一点,运维可高枕无忧
  • 读天下杂志读天下杂志社读天下编辑部简介
  • 王者荣耀游戏制作
  • 从零构建属于自己的GPT系列2:预训练中文模型加载、中文语言模型训练、逐行代码解读
  • During handling of the above exception, another exception occurred解决方案
  • vue项目实现生成一个简单二维码
  • 前端面试灵魂提问
  • 浅析智慧社区建设趋势及AI大数据监管平台方案设计
  • wsj0数据集原始文件.wv1.wv2转换成wav文件
  • Kanna库编写数据抓取代码示例
  • C# 线程(1)
  • 分布式运用之ELK企业级日志分析系统
  • 【C 语言经典100例】C 练习实例14 - 将一个正整数分解质因数
  • java基础之集合概览