[LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)
问题
给出一个元素以递增序列排序的单链表,将其转换为一棵高度平衡的二叉搜索树。
初始思路
二叉搜索树高度平衡,意味着左右子树的高度要平衡。根据二叉树左子树节点小于根节点,右子树节点大于根节点的性质:我们在待选节点中选择值为中位数的节点作为根节点,所有小于中位数的节点作为左子树,所有大于中位数的节点作为右子树,即可满足高度平衡的要求。由于题目给出的已经是排好序的单链表,我们只要每次选择中间的节点即可。然后通过递归处理左右子树,最终完成高度平衡二叉搜索树的构建。
最终完成代码如下:
class Solution {
enum ChildType
{
LEFT,
RIGHT
};
public:
TreeNode *sortedListToBST(ListNode *head)
{
if(!head)
{
return nullptr;
} ListNode* middle = FindMiddlePoint(head, nullptr); TreeNode* root = new TreeNode(middle->val); MakeSubTree(head, middle, root, LEFT);
MakeSubTree(middle->next, nullptr, root, RIGHT); return root;
} void MakeSubTree(ListNode* head, ListNode* end, TreeNode* parent, ChildType childType)
{
if(head == end)
{
return;
} ListNode* middle = FindMiddlePoint(head, end); TreeNode* treeNode = new TreeNode(middle->val); if(childType == LEFT)
{
parent->left = treeNode;
}
else
{
parent->right = treeNode;
} MakeSubTree(head, middle, treeNode, LEFT);
MakeSubTree(middle->next, end, treeNode, RIGHT);
} ListNode* FindMiddlePoint(ListNode* head, ListNode* end)
{
int length = ;
ListNode* node = head; while(node != end)
{
++length;
node = node->next;
} length /= ; node = head;
for(int i = ; i < length; ++i)
{
node = node->next;
} return node;
}
};
sortedListToBST
Judge Small和Judge Large皆顺利通过。
[LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)的更多相关文章
- LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14
108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...
- [Swift]LeetCode109. 有序链表转换二叉搜索树 | Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- [Leetcode 108]有序数组转二叉搜索树Convert Sorted Array to Binary Search Tree
[题目] 给出的升序排序的数组,个数必为奇数,要求形成二叉搜索(平衡)树. [思路] 辅助函数fun,[0,len]=>[0,mid-1]+[mid+1,len]. 当left>right ...
- LeetCode(109):有序链表转换二叉搜索树
Medium! 题目描述: 给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: ...
- [LeetCode] 109. Convert Sorted List to Binary Search Tree 把有序链表转成二叉搜索树
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- [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 bala ...
- Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst
Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
随机推荐
- Selenium webdriver 高级应用
对于这一段还蛮有感慨的,只想说,代码还是需要自己去敲的. 1. 改变用户代理 import org.junit.AfterClass; import org.junit.BeforeClass; im ...
- Java中直接输出一个类的对象
例如 package com.atguigu.java.fanshe; public class Person { String name; private int age; public Strin ...
- centos系统安装中文字体几种方法
我们知道centos是基于linux内核的这款系统默认是不带中文字体了,如果我们要使用中文字体就需要自行安装了,下面一起来看看吧. 前天有用户反应,生成的报到单中他的名字少了一个字.仔细检查了一下 ...
- mongodb3.0 性能測试报告 一
mongodb3.0 性能測试报告 一 mongodb3.0 性能測试报告 二 mongodb3.0 性能測试报告 三 測试环境: 服务器:X86 pcserver 共6台 cpu: 单颗8核 ...
- monkeyrunner 详细介绍
MonkeyRunner: monkeyrunner工具提供了一个API,使用此API写出的程序可以在Android代码之外控制Android设备和模拟器.通过monkeyrunner,您可以写出一个 ...
- [Angular 2] Child Router
Benefit to use child router is Angualr 2 then can lazy load the component on demand. Define a child ...
- cache 的设计与实现--转载
本文整理自一下两篇博客:http://my.oschina.net/ScottYang/blog/298727http://my.oschina.net/u/866190/blog/188712 Ca ...
- web03--session
1.创建session1.jsp <body> <form action="session2.jsp" method="post"> & ...
- jsp页面中定时的方法
$(function(){ totaladd(); //定时时触发的函数 setInterval(totaladd,3000);//设置定时1000=1秒 }); function totaladd( ...
- inline-block元素的空白间距解决方法
方法1 <ul><li>item1</li><li>item2</li><li>item3</li><li&g ...