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

資訊專欄INFORMATION COLUMN

[LeetCode] 109. Convert Sorted List to Binary Sear

dongfangyiyu / 2229人閱讀

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 which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted linked list: [-10,-3,0,5,9],

One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:

      0
     / 
   -3   9
   /   /
 -10  5
Solution
class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        return helper(head, null);
    }
    private TreeNode helper(ListNode head, ListNode tail) {
        if (head == tail) return null;
        ListNode fast = head, slow = head;
        while (fast != tail && fast.next != tail) {
            fast = fast.next.next;
            slow = slow.next;
        }
        TreeNode root = new TreeNode(slow.val);
        root.left = helper(head, slow);
        root.right = helper(slow.next, tail);
        return root;
    }
}

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

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

相關(guān)文章

  • leetcode109. Convert Sorted List to Binary Search

    摘要:題目要求給一個(gè)按照遞增順序排列的鏈表。將該鏈表轉(zhuǎn)化為平衡二叉樹(shù)。思路和代碼在這里需要注意的是,因?yàn)樘峁┑臄?shù)據(jù)結(jié)構(gòu)為鏈表,所以我們必須順序遍歷才能知道該鏈表的長(zhǎng)度以及該鏈表的中間位置。并依次遞歸左子節(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] Convert Sorted Array/List to Binary Sea

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

    wpw 評(píng)論0 收藏0
  • 109. Converted Sorted List to Binary Search Tree

    摘要:題目答案這里是不能等于也省去了把從中間分隔時(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. * p...

    plokmju88 評(píng)論0 收藏0
  • [LeetCode] Convert Sorted Array to Binary Search T

    摘要:思路根據(jù)的性質(zhì),問(wèn)題轉(zhuǎn)化為找一個(gè)里的中位數(shù),用一個(gè)函數(shù),一路找中點(diǎn),再通過(guò)前序遍歷的方法把起來(lái)代碼 Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order) array, Convert it to create a binarytree with m...

    willin 評(píng)論0 收藏0
  • [Leetcode-Tree] Convert Sorted Array to Binary Sea

    摘要:解題思路平衡二叉樹(shù),其實(shí)就是數(shù)組中間的數(shù)作為根,利用遞歸實(shí)現(xiàn)左子樹(shù)和右子樹(shù)的構(gòu)造。 Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST. 1.解題思路平衡二叉樹(shù),...

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

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

0條評(píng)論

閱讀需要支付1元查看
<