【leetcode刷题笔记】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.



题解:开始想到的方法比较偷懒,直接遍历一遍数组,把每个ListNode对应的值放到数组里面,然后把数组转换成BST,想来这个题的本意肯定不是这样。

自己也想到了bottom-up的方法,但是没想明白怎么个bottom-up法,真的是昨天做梦都在想=。=

今天早上终于弄懂了,用递归,不过这个递归比较难理解。

递归函数的定义是这样的 public TreeNode sortLisTreeNodeRecur(int size) ,参数size的意义可以理解为要建立的树的节点个数。用一个current变量作为linked list的游标。递归函数的实现如下:

 1 public TreeNode sortLisTreeNodeRecur(int size){
 2         if(size <= 0)
 3             return null;
 4
 5         TreeNode left = sortLisTreeNodeRecur(size/2);
 6         TreeNode root = new TreeNode(current.val);
 7         current = current.next;
 8         TreeNode right = sortLisTreeNodeRecur(size - size/2 - 1);
 9
10         root.left = left;
11         root.right = right;
12
13         return root;
14     }

可以看到,它先递归调用自己建立整个左子树,在这个过程中,current作为全局私有变量,在左子树建立完成以后,就自动移动到根节点对应的ListNode处了,所以建立完左子树后建立根节点,后移current,然后递归的建立整个右子树。

以Linked List = 1->2->3->4->5来画图说明:

代码如下:

 1 /**
 2  * Definition for singly-linked list.
 3  * public class ListNode {
 4  *     int val;
 5  *     ListNode next;
 6  *     ListNode(int x) { val = x; next = null; }
 7  * }
 8  */
 9 /**
10  * Definition for binary tree
11  * public class TreeNode {
12  *     int val;
13  *     TreeNode left;
14  *     TreeNode right;
15  *     TreeNode(int x) { val = x; }
16  * }
17  */
18 public class Solution {
19     private ListNode current;
20     public int getLength(ListNode head){
21         int answer = 0;
22         while(head != null){
23             answer++;
24             head = head.next;
25         }
26
27         return answer;
28     }
29     public TreeNode sortLisTreeNodeRecur(int size){
30         if(size <= 0)
31             return null;
32
33         TreeNode left = sortLisTreeNodeRecur(size/2);
34         TreeNode root = new TreeNode(current.val);
35         current = current.next;
36         TreeNode right = sortLisTreeNodeRecur(size - size/2 - 1);
37
38         root.left = left;
39         root.right = right;
40
41         return root;
42     }
43     public TreeNode sortedListToBST(ListNode head) {
44         current = head;
45         int size = getLength(head);
46         return sortLisTreeNodeRecur(size);
47     }
48 }

【leetcode刷题笔记】Convert Sorted List to Binary Search Tree

时间: 2024-12-12 01:53:33

【leetcode刷题笔记】Convert Sorted List to Binary Search Tree的相关文章

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

【leetcode刷题笔记】Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解:递归,树的高度 = max(左子树高度,右子树高度)+1: 代码如下: 1 /** 2 * Definition for binary tree 3 * public cla

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 本题大意:给定一个单向链表,构造出平衡二叉搜索树 解题思路:参考[一天一道Leet

109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST 这题和convert array t

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  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. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar