当前位置:网站首页>[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
边栏推荐
- 关于CS-3120舵机使用过程中感觉反应慢的问题
- Leetcode 430 flat a multilevel double linked list (DFS linked list)
- 2021-7-18 ROS笔记-xml语言相关
- [ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design
- 1.7 calibration of Px4 remote controller
- Px4 installation tutorial (VI) vertical fixed wing (tilting)
- QGC地面站使用教程
- 2021-3-1MATLAB写cnn的mnist数据库训练
- [leetcode] merge K ascending linked lists
- SAS discriminant analysis (Bayes criterion and proc discrim process)
猜你喜欢

Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack

Project_ Visual analysis of epidemic data based on Web Crawler

1.5 Px4 vehicle selection

2021-2-14 gephi learning notes

What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
![[cloud native | kubernetes] actual combat of ingress case](/img/99/1674f79e7ade0ec2f39e9b2c522d59.png)
[cloud native | kubernetes] actual combat of ingress case

Threejs: streamer effect encapsulation

detectron2训练自己的数据集和转coco格式

2021-2-14 gephi学习笔记

A brief history of neural network
随机推荐
2021-02-27image processing of MATLAB
薪的测试开发程序员们,你为何要走?和产品相互残杀到天昏地暗......
从解读 BDC 自动生成的代码谈起,讲解 SAPGUI 的程序组成部分试读版
2.0 detailed explanation of ROS and Px4 communication
【云原生 | Kubernetes篇】Ingress案例实战
[chess life] 01 life is like chess
Matlab digital operation function notes
2021-02-27MATLAB的图像处理
Uninstall mavros
2021-3-1MATLAB写cnn的mnist数据库训练
1.3 ROS 无人机简介
[mavros] mavros startup Guide
1.7 calibration of Px4 remote controller
2021-02-03美赛前MATLAB的学习笔记(灰色预测、线性规划)
Leetcode 2171 removing minimum number of magic beans (prefix and recommendation)
OCR文字识别经典论文详解
2.1、ROS+PX4仿真---定点飞行控制
threejs:如何获得几何体的boundingBox?
[ROS introduction] cmakelist Txt and packages XML interpretation
Leetcode 1248 count number of nice subarrays