亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

109. Converted Sorted List to Binary Search Tree

plokmju88 / 3089人閱讀

摘要:題目答案這里是不能等于也省去了把從中間分隔時(shí),需要添加的麻煩

題目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

答案:

/**
 * 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; }
 * }
 */
public class Solution {
    public TreeNode BST(ListNode head, ListNode tail) {
        ListNode slow = head, fast = head;
        if (head == tail) return null;
        
        //這里是fast不能等于tail,也省去了把listnode從中間分隔時(shí),需要添加null的麻煩!
        while (fast != tail && fast.next != tail) {
            slow = slow.next;
            fast = fast.next.next;
        }
        
        TreeNode root = new TreeNode(slow.val);
        root.left = BST(head, slow);
        root.right = BST(slow.next, tail);
        return root;
    }
    
    public TreeNode sortedListToBST(ListNode head) {
        if (head == null) return null;
        return BST(head, null);
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://www.ezyhdfw.cn/yun/64921.html

相關(guān)文章

  • leetcode109. Convert Sorted List to Binary Search

    摘要:題目要求給一個(gè)按照遞增順序排列的鏈表。將該鏈表轉(zhuǎn)化為平衡二叉樹。思路和代碼在這里需要注意的是,因?yàn)樘峁┑臄?shù)據(jù)結(jié)構(gòu)為鏈表,所以我們必須順序遍歷才能知道該鏈表的長度以及該鏈表的中間位置。并依次遞歸左子節(jié)點(diǎn)和右子節(jié)點(diǎn)。 題目要求 Given a singly linked list where elements are sorted in ascending order, convert i...

    高勝山 評(píng)論0 收藏0
  • [LeetCode] 109. Convert Sorted List to Binary Sear

    Problem Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in whi...

    dongfangyiyu 評(píng)論0 收藏0
  • 前端 | 每天一個(gè) LeetCode

    摘要:在線網(wǎng)站地址我的微信公眾號(hào)完整題目列表從年月日起,每天更新一題,順序從易到難,目前已更新個(gè)題。這是項(xiàng)目地址歡迎一起交流學(xué)習(xí)。 這篇文章記錄我練習(xí)的 LeetCode 題目,語言 JavaScript。 在線網(wǎng)站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公眾號(hào): showImg(htt...

    張漢慶 評(píng)論0 收藏0
  • leetcode部分題目答案之JavaScript版

    摘要:自己沒事刷的一些的題目,若有更好的解法,希望能夠一起探討項(xiàng)目地址 自己沒事刷的一些LeetCode的題目,若有更好的解法,希望能夠一起探討 Number Problem Solution Difficulty 204 Count Primes JavaScript Easy 202 Happy Number JavaScript Easy 190 Reverse Bi...

    alphahans 評(píng)論0 收藏0
  • [Leetcode] Convert Sorted Array/List to Binary Sea

    摘要:我們可以用和兩個(gè)值來限定子樹在鏈表中的位置,通過遞歸的方式,深入找到最左邊,然后開始順序遍歷鏈表鏈表當(dāng)前節(jié)點(diǎn)作為全局變量,這樣無論遞歸在哪我們都能拿到,同時(shí)建樹。代碼先遞歸的計(jì)算左子樹創(chuàng)造根節(jié)點(diǎn)最后遞歸的計(jì)算右子樹 Convert Sorted List to Binary Search Tree Given a singly linked list where elements ar...

    wpw 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<