leetcode || 109、Convert Sorted List to Binary Search Tree

problem:

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

Hide Tags

Depth-first Search Linked
List

题意:给定一个递增的单链表,将其转换成平衡查找二叉树,这道题是上道题的变形

thinking:

(1)平衡二叉树的概念:左右子树的高度差最大不能超过1,查找二叉树的概念:左孩子<父节点<右孩子

(2)二分法递归构造二叉树,只是单链表用二分法有点不方便,每次寻找中间结点需要将指针游走到中间位置。

对于只有一个元素的结点,该题可以将元素保存到数组中再处理,利用顺序存储随机访问的优势降低时间复杂度。

(3)模板函数解决形参类型vector<int>::iterator 太长,书写不方便

code:

class Solution {
public:
     TreeNode *sortedListToBST(ListNode *head) {
         vector<int> num;
         ListNode *ptr=head;
         while(ptr!=NULL)
         {
             num.push_back(ptr->val);
             ptr=ptr->next;
         }
         if(num.size()==0)
             return NULL;
         return make(num.begin(),num.end());
     }
protected:
      template<class it>
      TreeNode *make(it first,it last)
         {
             if(first==last)
                 return NULL;
             it loc = first+(last-first)/2;
             TreeNode *node = new TreeNode(*loc);
             node->left=make(first,loc);
             node->right=make(loc+1,last);
             return node;
         }  

  };
时间: 2024-10-01 02:33:07

leetcode || 109、Convert Sorted List to Binary Search Tree的相关文章

leetcode || 108、Convert Sorted Array to Binary Search Tree

problem: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Hide Tags Tree Depth-first Search 题意:将一个递增的序列 转换成一棵 平衡查找二叉树 thinking: (1)平衡二叉树的概念:左右子树的高度差最大不能超过1,查找二叉树的概念:左孩子<父节点<右孩子 (2)二分法递归构造二叉树 (3)模板函数解决

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

LeetCode解题报告——Convert Sorted List to Binary Search Tree &amp; Populating Next Right Pointers in Each Node &amp; Word Ladder

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. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth

【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 题目大意:给定一个排好序的数组,将它转变成一个平衡二叉搜索数 解题思路:平衡二叉搜索树的中序遍历为升序数组

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

LeetCode OJ 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. 把一个有序的数组转换为一颗平衡二叉搜索树.每次找到中间节点作为根节点,根节点左边部分转换为左子树,右边部分转换为右子树.代码如下: 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val;

【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. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by m

【一天一道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

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