`
hcx2013
  • 浏览: 83881 次
社区版块
存档分类
最新评论

Convert Sorted List to Binary Search Tree

 
阅读更多

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 {
	private ListNode h;
    public TreeNode sortedListToBST(ListNode head) {
        if (head == null) {
        	return null;
        }
        h = head;
        ListNode tmp = head;
        int cnt = 0;
        while (tmp != null) {
        	cnt++;
        	tmp = tmp.next;
        }
        return solve(0, cnt-1);
    }
	private TreeNode solve(int s, int e) {
		if (s > e) {
			return null;
		}
		int mid = (s+e)/2;
		TreeNode left = solve(s, mid-1);
		TreeNode root = new TreeNode(h.val);
		root.left = left;
		h = h.next;
		TreeNode right = solve(mid+1, e);
		root.right = right;
		return root;
	}
}

 

分享到:
评论

相关推荐

    cpp-算法精粹

    Convert Sorted List to Binary Search Tree LCA of BST Kth Smallest Element in a BST 二叉树的递归 Minimum Depth of Binary Tree Maximum Depth of Binary Tree Path Sum Path Sum II Binary Tree Maximum Path ...

    lrucacheleetcode-leetcode:leetcode

    lru缓存leetcode leetcode 大批 41. First Missing Positive 广度优先搜索 773. Sliding ...to ...Binary ...Convert Sorted List to Binary Search Tree 116. Populating Next Right Pointers in Each No

    LeetCode最全代码

    * [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...

    leetcodelintcode差异-LeetcodeJava:LeetcodeJava

    Binary Search Tree Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. public static TreeNode sortedListToBST(ListNode head) 思路 :递归实现:...

    折半查找 binarySearch

    The most common application of binary search is to find a specific value in a sorted list. To cast this in the frame of the guessing game (see Example below), realize that we are now guessing the ...

    Dictionary, SortedDictionary, SortedList 横向评测

    Dictionary, SortedDictionary, SortedList 横向评测

    leetcode卡-leetcode:利特码解决方案

    Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: ...

    用sortedlist,vsto合并两份excel

    网上找了好久,都没有具体用sortedlist合并excel的实例,自己写了一个,还用了vsto,希望能给初学者提供帮助>.

    SortedList类

    SortedList最合适对一列健/值对 进行排序,在排序时,是对键进行排序,SortedList 是 Hashtable 和 Array 的混合。当使用 Item 索引器属性按照元素的键访问元素时,其行为类似于 Hashtable。当使用 GetByIndex 或 ...

    java实现别踩白块儿源码-SortedList:用Java编写的SortedList的实现。可以与实现Comparable接口的对象一起使用

    java实现别踩白块儿源码SortedList Sorted List的实现,它扩展了ArrayList。 它是使用Comparator对象构造的,该对象可以将两个对象进行比较,从而使SortedList可以将其元素按升序或降序排序。 当且仅当要使用的对象...

    SortedList.java

    SortedList.java

    C#中List和SortedList的简介

    今天小编就为大家分享一篇关于C#中List和SortedList的简介,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

    c# 中的arrlist queue HashtableTest SortedList stack 集合

    c# 中的arrlist queue HashtableTest SortedList stack 集合

    c#的sortedlist使用方法

    SortedList最合适对一列健/值对 进行排序,在排序时,是对键进行排序,SortedList 是 Hashtable 和 Array 的混合。当使用 Item 索引器属性按照元素的键访问元素时,其行为类似于 Hashtable。当使用 GetByIndex 或 ...

    Algorithms_Binary_Sorted_Tree_

    Algorithms_Binary_Sorted_Tree_

    常见的字典数据结构实现

    实现常见的字典数据结构,包括Binary Search Tree/Red-Black Tree/Balanced Tree/Skip List/Sorted Array

    leetcode添加元素使和等于-LeetCode:leetcode

    leetcode添加元素使和等于 LeetCode leetcode 指针类型题目 1. 链表 函数参数传入的链表都没有链表的头,从第一个有数据的节点开始 函数返回一个链表,这个链表也没有头 2. 内存分配 二维数组内存分配 ...

    Collectionjs:SortedSet,SortedList,Queue,ArrayList,LinkedList,TreeSet,HashMap

    它可以在 nodejs 和浏览器中使用。 var sets=new Collection.SortedSet(); sets.add('z');...var list=new Collection.SortedList(); list.compare=function(a,b) { if(a.name<b>b.name) return

    lrucacheleetcode-LeetCode_Note:leetcode个人笔记

    lru缓存leetcode LeetCode_Note leetcode 个人笔记 问题清单 [1_two_sum.cpp] [10_regular-expression-matching.cpp] [100_same-tree.cpp] [1001_sorted-merge-lcci.cpp] [101_对称树.cpp] ...[108_convert-sorted

    AVL和红黑树性能对比

    Binary search tree (BST) based data structures, such as AVL trees, red-black trees, and splay trees, are often used in system software, such as operating system kernels. Choosing the right kind of ...

Global site tag (gtag.js) - Google Analytics