- 浏览: 183790 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 { public TreeNode sortedListToBST(ListNode head) { if(head == null) return null; ListNode fast = head; ListNode slow = head; ListNode pre = null; while(fast != null && fast.next != null) { pre = slow; slow = slow.next; fast = fast.next.next; } ListNode rightHead = slow.next; TreeNode root = new TreeNode(slow.val); if(pre != null) { pre.next = null; root.left = sortedListToBST(head); } root.right = sortedListToBST(rightHead); return root; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 500Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 430Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 581Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 677Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 845Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 708For a undirected graph with tre ...
相关推荐
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...