day81(2.9)——leetcode面试经典150

108. 将有序数组转换为二叉搜索树

108. 将有序数组转换为二叉搜索树

题目:

题解:

java 复制代码
/**
 * 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 {
    TreeNode conTree(int l, int r, int[] nums) { 
        if(l>r) {
            return null;
        }
        int mid = (l+r)/2;
        TreeNode node = new TreeNode(nums[mid]);
        node.left = conTree(l,mid-1,nums);
        node.right = conTree(mid+1,r,nums);
        return node;
    }
    
    public TreeNode sortedArrayToBST(int[] nums) {
        return conTree(0,nums.length-1,nums);
    }
}

148. 排序链表

148. 排序链表

题目:

题解:

java 复制代码
/**
 * 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; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        if(head==null) {
            return head;
        }
        ListNode cur = head;
        List<Integer> list = new ArrayList<>();
        while(cur!=null) {
            list.add(cur.val);
            cur = cur.next;
        }
        list.sort(null);
        ListNode node = new ListNode();
        cur = node;
        for(int i=0;i<list.size();i++) {
            cur.val=list.get(i);
            if(i!=list.size()-1) {
                cur.next = new ListNode();
            }
            else {
                cur.next = null;
            }
            cur = cur.next;
        }
        return node;
    }
}
相关推荐
geovindu14 小时前
python:DecisionTree
开发语言·后端·python·算法
Cosolar14 小时前
从 Java 21 升到 25、Spring Boot 3 升 4:一次可复现的迁移踩坑实录
java·后端·面试
ShineWinsu15 小时前
对于Qt:事件的解析
c++·qt·面试·笔试·gui·客户端·事件
geovindu16 小时前
python: Decision Tree Regressor
开发语言·后端·python·算法·决策树
我叫黑大帅18 小时前
搜“张三”把备注也带出来了?不用加高级筛选,如何让一个搜索框实现「选项直达」!
前端·javascript·面试
AllData公司负责人18 小时前
实战二:AllData大模型与AI数据底座-数据传输平台-DBSyncer,搭建 Http 接口同步至 Doris,API 业务数据自动拉取入数仓
大数据·数据库·数据仓库·人工智能·算法·http·doris
武子康19 小时前
火系被选七成,就应该削弱吗?
人工智能·算法·机器学习
夜不会漫长20 小时前
C++:C++11特性
开发语言·c++·算法
小酒星小杜20 小时前
年终总结,这可能是程序员最后一个冬天了
人工智能·面试·程序员