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

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;
	}
}

 

分享到:
评论

相关推荐

    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

    python-leetcode-109-Convert-Sorted-List-to-Binary-Search-Tree

    python python_leetcode_109_Convert_Sorted_List_to_Binary_Search_Tree

    js-leetcode题解之109-convert-sorted-list-to-binary-search-tree.js

    javascript js_leetcode题解之109-convert-sorted-list-to-binary-search-tree.js

    常见算法题答案及解析

    30. Convert Sorted List to Binary Search Tree:将有序链表转换为高度平衡的二叉搜索树。 31. Binary Tree Maximum Path Sum:二叉树中的最大路径和。 32. Binary Tree Upside Down:将二叉树进行上下翻转。 五、...

    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) 思路 :递归实现:...

    java-leetcode题解之第109题有序链表转换二叉搜索树.zip

    在本压缩包中,我们关注的是Java编程语言与LeetCode平台上的第109题——有序链表转换为二叉搜索树(Convert Sorted List to Binary Search Tree)。这是一道涉及数据结构与算法的问题,主要涵盖了链表操作和二叉搜索...

    Leetcode答案(c++版)

    **1.9 Convert Sorted List to Binary Search Tree (109)** - **问题描述**:给定一个链表,将其转换为平衡二叉搜索树。 - **解题思路**: - 先找到链表的中间节点作为根节点。 - 使用递归方式处理左子树和右子树...

    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 其中算法,主要是以下几种: ...

    Leetcode book刷题必备

    30. Convert Sorted List to Balanced Binary Search Tree:将有序链表转换成平衡的二叉搜索树。 31. Binary Tree Maximum Path Sum:二叉树中的最大路径和。 32. Binary Tree Upside Down:将一个二叉树进行翻转。 ...

    Leetcode题目+解析+思路+答案.pdf

    - **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to ...

    lrucacheleetcode-LeetCode:LeetCode刷题

    把字符串转换成整数(Convert a string to an integer) 2018.10.8 树的子结构(Substructure of the tree) 2018.10.8 旋转数组的最小数字(Rotate The Smallest Number of Arrays) 2018.10.9 二叉搜索树的后序...

    VB编程资源大全(英文源码 文件)

    shortcut.zip How to create a shortcut to another file<END><br>36 , CountLines.zip How to count the number of lines in a text file<END><br>37 , f_165.zip How to create a directory tree like ...

    算法-leetcode-剑指offer上的题很多

    - **整数转换(Convert Integer A to Integer B)**: 将一个整数转换为另一个整数。 - **阶乘末尾0的个数(Factorial Trailing Zeroes)**: 计算一个整数阶乘结果末尾有多少个零。 - **唯一二叉搜索树(Unique Binary ...

    Facebook 最新面试题总结

    Convert Binary Search Tree to Sorted Doubly Linked List(将二叉搜索树转换为排序的双链表)**: - 需要将二叉搜索树转换为双向链表,且链表中的节点按照排序顺序排列。 5. **LeetCode 56. Merge Intervals...

    python3.6.5参考手册 chm

    The plistlib module: A Property-List Parser ctypes Enhancements Improved SSL Support Deprecations and Removals Build and C API Changes Port-Specific Changes: Windows Port-Specific Changes: Mac OS...

Global site tag (gtag.js) - Google Analytics