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

 

    这题是将链表变成二叉树,比较麻烦的遍历过程,因为链表的限制,所以深度搜索的顺序恰巧是链表的顺序,通过设置好递归函数的参数,可以在深度搜索时候便可以遍历了。
 
TreeNode * help_f(ListNode *&curList,int lft,int rgt)

全部代码:

#include <iostream>
using namespace std; /**
* Definition for singly-linked list.
*/
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; /**
* Definitiosn for binary tree
*/
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
int len=;
ListNode * p = head;
while(p!=NULL){
len++;
p=p->next;
}
// cout<<len<<endl;
return help_f(head,,len-);
} TreeNode * help_f(ListNode *&curList,int lft,int rgt)
{
if(lft>rgt) return NULL;
int mid=(lft+rgt)/;
TreeNode *lftCld = help_f(curList,lft,mid-);
TreeNode *parent =new TreeNode(curList->val);
parent->left=lftCld;
curList=curList->next;
parent->right=help_f(curList,mid+,rgt);
return parent;
}
}; int main()
{
ListNode n1();
Solution sol;
sol.sortedListToBST(&n1);
return ;
}

[LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索的更多相关文章

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

  2. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  3. [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 ...

  4. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  5. 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 ...

  6. LeetCode: Convert Sorted List to Binary Search Tree 解题报告

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  7. LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  8. 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 ...

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

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...

随机推荐

  1. 转:对比python 链接 neo4j 驱动,py2neo 和 neo4j-driver 和 neo4jrestclient

    Comparing Neo4j driver, py2neo and neo4jrestclient with some basic commands using the Panama Papers  ...

  2. LAMP 一键部署

    LAMP 一键部署 部署http #!/bin/bash ### global variables export lamp_repo=http://192.168.1.5/lamp/ export l ...

  3. 【104】Maven3.5.0结合eclipse使用,提示Lambda expressions are allowed only at source level 1.8 or above错误的解决方法

    错误重现 我的机器上安装了 maven 3.5.0,在 eclipse 中创建 maven 项目.pom.xml配置如下: <project xmlns="http://maven.a ...

  4. 5分钟带你快速理解Http协议

    HTTP协议 什么是HTTP协议 HTTP(Hyper Text Transfer Protocol)协议又叫超文本传输协议,是建立在TCP/IP协议之上的用来传递数据的协议.它不涉及数据包的传递,主 ...

  5. HDU:4417-Super Mario(离线线段树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...

  6. UVA:11297-Census(二维线段树)

    Census Time Limit: 8 sec Description This year, there have been many problems with population calcul ...

  7. 华东交通大学2018年ACM“双基”程序设计竞赛 D

      摸鱼之王MIKU酱想去埃及玩,需要一个人陪同.小新和小磊都想陪MIKU酱一起去,但名额只有一个.所以小磊和小新决定用一个小游戏来决定谁和MIKU酱出去玩.    游戏的道具是21张塔罗牌,塔罗牌分 ...

  8. 实验一 查看CPU和内存,用机器指令和汇编指令编程

    (1):使用debug,将下面的程序段写入内存,逐条执行,观察每条指令执行后,CPU中相关寄存器中内存的变化. 机器码        汇编指令 b8 20 4e     mov ax,4E20H 05 ...

  9. SpringMVC controller接收的中文参数乱码

    CharacterEncodingFilter只对POST请求有用,GET请求的需要对你运行的tomcat 目录conf/server.xml文件中<Connector connectionTi ...

  10. 网络抢票黄牛,大部分是骗人的。公布一个骗钱黄牛,QQ:2233261390,QQ群:29443597,支付页面:https://me.alipay.com/q336

    想着给女友买张回广州的回程火车票,抢了3天也没弄到.情急之下找了网络上所谓的黄牛.结果上当受骗.具体经过是这样的: 对方承诺抢到再付款.于是等他抢到后截图给我看,而且可以远程到他的机器去看,我也确实远 ...