题目

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. 堆排序之Java实现

    堆排序之Java实现 代码: package cn.com.zfc.lesson21.sort; /** * * @title HeapSort * @describe 堆排序 * @author 张 ...

  2. Develop with asyncio部分的翻译

    Develop with asyncio 异步程序和普通的连续程序(也就是同步程序)是很不一样的,这里会列出一些常见的陷阱,并介绍如何去避开他们. Debug mode of asyncio 我们用a ...

  3. 深入理解webpack

    什么是Webpack WebPack可以看做是模块打包机:它做的事情是,分析你的项目结构,找到JavaScript模块以及其它的一些浏览器不能直接运行的拓展语言(Scss,TypeScript等),并 ...

  4. Linux学习笔记06—系统用户及用户组的管理

    一.认识/etc/passwd和/etc/shadow 1./etc/passwd 由 ‘:’ 分割成7个字段,每个字段的具体含义是: 用户名 存放账号的口令:现在存放在/etc/shadow下,在这 ...

  5. JS funtion()中URL不跳转后台action问题

    JS funtion()中URL不跳转后台action问题 今天遇到一个百思不得其解的问题,到现在解决了,但是仍然不知道所以然(估计是因为域名不一致导致的),记录一下 $.get(actionUrl, ...

  6. Unity3D实践系列01,创建项目

    下载并安装Unity5软件客户端. 打开软件,注册Unity帐号,并用注册帐号登录. 点击"创建Project"按钮. 把项目命名为"My First Unity Pro ...

  7. 在ASP.NET MVC中实现区域或城市选择

    每次在"万达影城"网上购票总会用到左上角选择城市的功能.如下: 今天就在ASP.NET MVC中实现一下.我想最好的方式应该是写一个插件,但自己在这方面的功力尚欠缺,如果大家在这方 ...

  8. 数据连接工具DbVisualizer的使用

    一.下载,DbVisualizer Pro 64位 9.0.2 绿色特别版 http://www.cr173.com/soft/103766.html 二.中文乱码 在配置数据库连接时的Databas ...

  9. 【Devops】【docker】【CI/CD】docker启动的Jenkins容器 - 系统管理 - 全局工具配置 - 自动安装JDK、Maven、Git、Docker

    本篇适用于jenkins是启动的docker容器,自动安装JDK  Maven  Git   Docker等全局工具 ========================================= ...

  10. Cocos2d-x之CCMenu

    from://http://blog.linguofeng.com/archive/2012/11/14/cocos2d-x-CCMenu.html Cocos2d-x之CCMenu Cocos2dx ...