<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ?LeetCode刷題實戰(zhàn)109:有序鏈表轉換二叉搜索樹

          共 1703字,需瀏覽 4分鐘

           ·

          2020-11-30 22:36

          算法的重要性,我就不多說了吧,想去大廠,就必須要經過基礎知識和業(yè)務邏輯面試+算法面試。所以,為了提高大家的算法能力,這個公眾號后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

          今天和大家聊的問題叫做?有序鏈表轉換二叉搜索樹,我們先來看題面:
          https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/
          Given the head of 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.

          題意


          給定一個單鏈表,其中的元素按升序排序,將其轉換為高度平衡的二叉搜索樹。
          本題中,一個高度平衡二叉樹是指一個二叉樹每個節(jié)點 的左右兩個子樹的高度差的絕對值不超過 1。

          樣例

          解題


          因為鏈表是有序的,我們遞歸時每次把中間的數(shù)加為節(jié)點進行構造二叉樹就可以了,這個每次找鏈表的中間節(jié)點依舊可以使用快慢指針的方法進行找。找中間節(jié)點和構造二叉樹便是此題的重點啦,找中間的節(jié)點也可以使用數(shù)組,就是把鏈表的數(shù)據都存到數(shù)組中,然后每次找中間的節(jié)點。


          class?Solution?{
          ????public?TreeNode sortedListToBST(ListNode head) {
          ????????if(head == null) return?null;
          ????????return?createTree(head, null);
          ????}
          ????public?TreeNode createTree(ListNode head, ListNode tail) {
          ????????if(head == tail ) return?null;
          ????????ListNode fast = head;
          ????????ListNode slow = head;
          ????????while(fast != tail && fast.next != tail) {
          ????????????fast = fast.next.next;
          ????????????slow = slow.next;
          ????????}
          ????????TreeNode node = new?TreeNode(slow.val);
          ????????node.left = createTree(head, slow);
          ????????node.right = createTree(slow.next, tail);
          ????????return?node;
          ????}
          }


          好了,今天的文章就到這里,如果覺得有所收獲,請順手點個在看或者轉發(fā)吧,你們的支持是我最大的動力。


          上期推文:


          LeetCode1-100題匯總,希望對你有點幫助!

          LeetCode刷題實戰(zhàn)101:對稱二叉樹

          LeetCode刷題實戰(zhàn)102:二叉樹的層序遍歷

          LeetCode刷題實戰(zhàn)103:二叉樹的鋸齒形層次遍歷

          LeetCode刷題實戰(zhàn)104:二叉樹的最大深度

          LeetCode刷題實戰(zhàn)105:從前序與中序遍歷序列構造二叉樹

          LeetCode刷題實戰(zhàn)106:從中序與后序遍歷序列構造二叉樹
          LeetCode刷題實戰(zhàn)107:二叉樹的層次遍歷 II
          LeetCode刷題實戰(zhàn)108:將有序數(shù)組轉換為二叉搜索樹

          瀏覽 50
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  一区二区三区无码专区 | 日本一级A片在线观看视频 | 性XXXX丰满孕妇XXXX另类 | 99久久夜色精品国产亚洲 | 黄色一级大片在线观看 |