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;
    }
}
相关推荐
徐小夕4 小时前
pxcharts Ultra V2.3更新:多维表一键导出 PDF,渲染兼容性拉满!
vue.js·算法·github
CoovallyAIHub5 小时前
OpenClaw一脚踩碎传统CV?机器终于不再只是看世界
深度学习·算法·计算机视觉
zone77395 小时前
003:RAG 入门-LangChain 读取图片数据
后端·python·面试
CoovallyAIHub6 小时前
仅凭单目相机实现3D锥桶定位?UNet-RKNet破解自动驾驶锥桶检测难题
深度学习·算法·计算机视觉
zone77396 小时前
002:RAG 入门-LangChain 读取文本
后端·算法·面试
青青家的小灰灰6 小时前
从入门到精通:Vue3 ref vs reactive 最佳实践与底层原理
前端·vue.js·面试
得物技术7 小时前
得物社区搜推公式融合调参框架-加乘树3.0实战
算法
over6977 小时前
从 URL 输入到页面展示:一次完整的 Web 导航之旅
前端·面试·架构
飞哥的AI笔记7 小时前
为什么 OpenClaw 在实时推送场景下选择拥抱 WebSocket?
面试
SuperEugene7 小时前
Vue状态管理扫盲篇:状态管理中的常见坑 | 循环依赖、状态污染与调试技巧
前端·vue.js·面试