当前位置:网站首页>[leetcode] ordered linked list transformation binary search tree
[leetcode] ordered linked list transformation binary search tree
2022-06-11 01:49:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
- Ordered list transform binary search tree
https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/ - analysis
This problem has a train of thought immediately :
Find the midpoint of the linked list 、 Cut into two linked lists , Construct trees , The left node of the tree points to the left part of the linked list , The right node of the tree points to the right part of the linked list , And then recursive processing .
One thing to note is that : When the linked list is divided into two parts , Take the midpoint as the root node of the tree . I made this mistake before , Take the midpoint as the root node of the tree , Have put the midpoint in the right linked list , The result is redundant data . - Realization
public TreeNode sortedListToBST(ListNode head) {
if (head == null) {
return null;
}
if (head.next == null) {
return new TreeNode(head.val);
}
// routine , Three pointers to find the midpoint of the linked list ,pre Is the midpoint ,slow To the right of the midpoint
ListNode slow = head, fast = head, prev = null;
while (fast != null && fast.next != null) {
prev = slow;
slow = slow.next;
fast = fast.next.next;
}
// To the right of the midpoint slow
// To the left of the midpoint head
prev.next = null;
TreeNode left = sortedListToBST(head);
// You need to pay attention here , Recursion from the right side down requires that you remove the current value , Point to show Next
TreeNode right = sortedListToBST(slow.next);
// Current node as the midpoint of the tree
TreeNode treeNode = new TreeNode(slow.val);
treeNode.left = left;
treeNode.right = right;
return treeNode;
}
LeetCode Time consuming :0ms
- summary
- Find the double pointer at the midpoint of the linked list ( Three pointers ) Law , Quick start
- According to the idea of solving problems recursively , When dealing with recursion at this level , Some details need to be considered comprehensively
边栏推荐
- 2021-2-14 gephi learning notes
- China-open-ssl编译的一些记录
- Leetcode search questions
- Is the SQL query result different from what you expected? Mostly "null" is making trouble
- Multi interest recall model practice | acquisition technology
- Leetcode divide and conquer method
- 2.1、ROS+PX4仿真---定点飞行控制
- Leetcode 1574 shortest subarray to be removed to make array sorted
- Lazy singleton mode
- [interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
猜你喜欢
随机推荐
Leetcode 1814 count nice pairs in an array (recommended by map)
【云原生 | Kubernetes篇】Ingress案例实战
Kubernetes binary installation (v1.20.15) (VII) plug a work node
On permutation and combination in probability and statistics
Leetcode 652 find duplicate subtrees (recommended by DFS)
腾讯云数据库TDSQL-大咖论道 | 基础软件的过去、现在、未来
2.2、ROS+PX4仿真多点巡航飞行----正方形
[leetcode] delete duplicate Element II in the sorting linked list
[recommended by Zhihu knowledge master] castle in UAV - focusing on the application of UAV in different technical fields
[path planning] week 1: hodgepodge
[VBA Script] extract the information and pending status of all annotations in the word document
Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
What if ROS lacks a package
[path planning] week 1: Path Planning open source code summary (ROS) version
Daily problem essay | 21.11.29: use resttemplate to call external put request, and prompt '400 bad request'
2021-07-18 ROS笔记-基础和通讯
负数+0+正数
Leetcode 698 partition to K equal sum subsets (DFS pruning)
LeetCode 1010 Pairs of Songs With Total Durations Divisible by 60 (hash)
2.1 ros+px4 simulation - Fixed Point flight control









![[ROS] review of 2021 ROS Summer School](/img/1c/588d29b60071628c7c9fdce17e8b84.jpg)