博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Jan 28 - Convert Sorted List To BST; Linked List; Tree; DFS; DAC;
阅读量:4550 次
发布时间:2019-06-08

本文共 3027 字,大约阅读时间需要 10 分钟。

Using the same DAC idea of previous problem. Difference is that we need to do the pointed operation of linked list rather than just change the index of array. If we find the middle position of the list, get the value of the list node, and get the list node (prev) ahead of it and then set prev.next = null. Then the left child node should be the in the remaining list ahead of current list node. Similarly, the right child node should be found in the left list part of current list node.

Code:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } *//** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public TreeNode sortedListToBST(ListNode head) {        ListNode cur = head;        if(cur == null) return null;        int val = cur.val;        TreeNode node = new TreeNode(val);        if(cur.next == null) return node;        if(cur.next.next == null) node.right = sortedListToBST(cur.next);        else{            int i = 0;            while(cur != null){                cur = cur.next;                i++;            }            int j = 0;            cur = head;            ListNode prev = head;            while(j < i/2){                prev = cur;                cur = cur.next;                j++;            }            val = cur.val;            node = new TreeNode(val);            prev.next = null;            node.left = sortedListToBST(head);            node.right = sortedListToBST(cur.next);        }        return node;    }}

 Here is the improvement: When we look through the current linked list from the head to the last node, in the end we can find the middle node the node ahead of middle node at the same time.

 

Code:

/** * Definition for singly-linked list. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int x) { val = x; } * } *//** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public TreeNode sortedListToBST(ListNode head) {        ListNode cur = head;                if(cur == null) return null;        int val = cur.val;        TreeNode node = new TreeNode(val);        if(cur.next == null) return node;        if(cur.next.next == null) node.right = sortedListToBST(cur.next);        else{            ListNode half = head;            ListNode prev = head;            while(cur != null && cur.next != null){                prev = half;                half = half.next;                cur = cur.next.next;            }            val = half.val;            node = new TreeNode(val);            prev.next = null;            node.left = sortedListToBST(head);            node.right = sortedListToBST(half.next);        }        return node;    }}

 

转载于:https://www.cnblogs.com/5683yue/p/5168347.html

你可能感兴趣的文章
mysql删除重复数据
查看>>
文件下载工具类
查看>>
Python 定义自己的常量类
查看>>
C++读取文本文件
查看>>
Python 字典排序
查看>>
sql中写标量函数生成大写拼音首字母
查看>>
ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
查看>>
服务器返回状态码说明
查看>>
GitHub for Windows提交失败“failed to sync this branch”
查看>>
linux 安装 git
查看>>
Margin
查看>>
完成登录与注册页面的前端
查看>>
centos 源码安装php7
查看>>
Log4j详细教程
查看>>
UVa-1368-DNA序列
查看>>
ConfigParser模块
查看>>
如何开发优质的 Flutter App:Flutter App 软件测试指南
查看>>
决胜Flutter 第一章 熟悉战场
查看>>
如何开发优质的 Flutter App:Flutter App 软件调试指南
查看>>
决胜经典算法之冒泡排序
查看>>