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; } }
相关推荐
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 python_leetcode_109_Convert_Sorted_List_to_Binary_Search_Tree
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:将二叉树进行上下翻转。 五、...
* [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]...
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题——有序链表转换为二叉搜索树(Convert Sorted List to Binary Search Tree)。这是一道涉及数据结构与算法的问题,主要涵盖了链表操作和二叉搜索...
**1.9 Convert Sorted List to Binary Search Tree (109)** - **问题描述**:给定一个链表,将其转换为平衡二叉搜索树。 - **解题思路**: - 先找到链表的中间节点作为根节点。 - 使用递归方式处理左子树和右子树...
Convert Sorted Array to Binary Search Tree string and search First Bad Version Dynamic Programing *** Climbing Stairs Set Matrix Zeroes API System.arrayCopy 刷题顺序 TOP100 其中算法,主要是以下几种: ...
30. Convert Sorted List to Balanced Binary Search Tree:将有序链表转换成平衡的二叉搜索树。 31. Binary Tree Maximum Path Sum:二叉树中的最大路径和。 32. Binary Tree Upside Down:将一个二叉树进行翻转。 ...
- **Convert Sorted List/Array to Binary Search Tree**:将有序列表或数组转换为二叉搜索树。 - **Path Sum II**:寻找所有从根节点到叶节点的路径,其路径和等于给定的目标值。 - **Flatten Binary Tree to ...
把字符串转换成整数(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 二叉搜索树的后序...
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 ...
- **整数转换(Convert Integer A to Integer B)**: 将一个整数转换为另一个整数。 - **阶乘末尾0的个数(Factorial Trailing Zeroes)**: 计算一个整数阶乘结果末尾有多少个零。 - **唯一二叉搜索树(Unique Binary ...
Convert Binary Search Tree to Sorted Doubly Linked List(将二叉搜索树转换为排序的双链表)**: - 需要将二叉搜索树转换为双向链表,且链表中的节点按照排序顺序排列。 5. **LeetCode 56. Merge Intervals...
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...