题目

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

题解

之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法。但是构造树的方法不是由顶至下了,是要由低至上的建立。

代码如下:

 1     static ListNode h;
 2  
 3     public TreeNode sortedListToBST(ListNode head) {
 4         if (head == null)
 5             return null;
 6             
 7         h = head;
 8         
 9         int len = 0;
         ListNode temp = head;
         while(temp != null){
             len++;
             temp = temp.next;
         }   
         return sortedListToBST(0, len - 1);
     }
  
     public TreeNode sortedListToBST(int start, int end) {
         if (start > end)
             return null;
         int mid = (start + end) / 2;
         TreeNode left = sortedListToBST(start, mid - 1);
         TreeNode root = new TreeNode(h.val);
         root.left = left;
         h = h.next;
         TreeNode right = sortedListToBST(mid + 1, end);
         root.right = right;
  
         return root;
     }

Reference: http://www.programcreek.com/2013/01/leetcode-convert-sorted-list-to-binary-search-tree-java/

Convert Sorted List to Binary Search Tree leetcode java的更多相关文章

  1. Convert Sorted Array to Binary Search Tree leetcode java

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  2. Convert Sorted List to Binary Search Tree [LeetCode]

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  3. Convert Sorted Array to Binary Search Tree || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

  4. Convert Sorted Array to Binary Search Tree——LeetCode

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...

  5. LeetCode算法题-Convert Sorted Array to Binary Search Tree(Java实现)

    这是悦乐书的第166次更新,第168篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第25题(顺位题号是108).给定一个数组,其中元素按升序排序,将其转换为高度平衡的二叉 ...

  6. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  7. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  8. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  9. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

随机推荐

  1. 【WIN10】WIN2D——基本圖形的繪製

    DEMO下載地址:http://yunpan.cn/c3iNuHFFAcr8h (提取码:8e48) 先看一個截圖: 繪製了一些基本形狀. DEMO的繪製代碼都非常簡單,不想在博客裡細說了,看代碼更為 ...

  2. [java] 虚拟机(JVM)底层结构详解[转]

    本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 在以前的博客里面,我们介绍了在java领域中大部分的知识点,从最基础的java最基本语法到 ...

  3. 微信小程序swiper高度自适应,swiper的子元素高度不固定

    小程序 swiper 组件默认高度150px,并且如果子元素过高,swiper不会自适应高度 解决方案一: (总体来说不够完美,适合满屏滑动) 如果不是满屏的状态,用scroll-view IOS滑动 ...

  4. 狗日的系统之家下载的Windows 10 1803/1809系统不干净,捆绑自动安装腾讯关键等软件

    特此记录一下,如果网友看到这篇文章请不要下载它们家的,捆绑软件,并且安装自动设置了账号,这还不是修改,是啥? 我们都知道现在iso文件基本都是网友自行制作的,从微软下载的文件自行制作成iso,也就是现 ...

  5. rsync使用sudo权限

    1.在etc/sudoers增加,比如www-data这个账户的 www-data ALL=NOPASSWD:/usr/bin/rsync 2.使用时增加--rsync-path="sudo ...

  6. .Net 环境下C# 通过托管C++调用本地C++ Dll文件

     综述 : 本文章介绍.Net 环境下C# 通过托管C++调用本地C++ Dll文件, 示例环境为:VS2010, .Net4.0, Win7. 具体事例为测试C++, C#, 及C#调用本地C++D ...

  7. One-wire Demo on the STM32F4 Discovery Board

    One-wire Demo on the STM32F4 Discovery Board Some of the devs at work were struggling to get their s ...

  8. USBDM Debugger interface for Freescale RS08,HCS08,HCS12,Coldfire and ARM-Kinetis Devices.

    Introduction USBDM is a debugger hardware interface for a range of Freescale microcontrollers. It is ...

  9. Linux kernel AIO

    http://blog.csdn.net/abcd1f2/article/details/47440087

  10. MFC之菜单

    1菜单与菜单项的操作 //获取菜单指针----CWnd::GetMenu() //GetSubMenu()获取子菜单 /CheckMenuItem()加入/取消标记 GetMenu()->Get ...