网站建设制作设计开发,删除wordpress网页无用,wordpress主题视频站,企业云邮箱给定一个单链表的头节点 head #xff0c;其中的元素 按升序排序 #xff0c;将其转换为 平衡 二叉搜索树。 示例 1: 输入: head [-10,-3,0,5,9]
输出: [0,-3,9,-10,null,5]
解释: 一个可能的答案是[0#xff0c;-3,9#xff0c;-10,null,5]#xff0c;它表示所示的高度…给定一个单链表的头节点 head 其中的元素 按升序排序 将其转换为 平衡 二叉搜索树。 示例 1: 输入: head [-10,-3,0,5,9]
输出: [0,-3,9,-10,null,5]
解释: 一个可能的答案是[0-3,9-10,null,5]它表示所示的高度平衡的二叉搜索树。示例 2:
输入: head []
输出: []提示:
head 中的节点数在[0, 2 * 104] 范围内-105 Node.val 10
思路先获取到链表的长度然后去递归构造树即可每次构造的树节点永远是链表或子链表的中心但是由于是单向链表所以每次获取链表中的节点的时候就会导致每次都从头开始可以用循环链表改善如果要构造的节点的坐标大于length/2的时候就next length -index次然后递归构造设置临界条件即可若length为0就是无节点如果length为1就是叶子节点。然后上代码
/*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode() {}* ListNode(int val) { this.val val; }* ListNode(int val, ListNode next) { this.val val; this.next next; }* }*/
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val val;* this.left left;* this.right right;* }* }*/
class Solution {ListNode temp;public TreeNode sortedListToBST(ListNode head) {temp head;// 思路就是取链表的中心节点作为总树或子树的根节点然后循环、递归int length getListLength(head);return buildTree(0, length);}public TreeNode buildTree(int start ,int length) {int i 0;ListNode t temp;while (i start length/2) {t t.next;i;}// 如果是0直接为nullif (length 0) return null;// 如果length为1的时候直接返回因为它已经是树叶节点了if (length 1) return new TreeNode(t.val, null, null);// 遍历到中心节点就构造节点return new TreeNode(t.val, buildTree(start, length/2), buildTree(start length/2 1, length-1-length/2));}// 获取节点总节点数public int getListLength(ListNode head) {int length 0;while(head ! null) {length;head head.next;}return length;}}
快慢指针也是解决中间值问题的一个快速的解决办法思路相同只是取中间值的方法不同。
class Solution {public TreeNode sortedListToBST(ListNode head) {return buildTree(head, null);}public TreeNode buildTree(ListNode left, ListNode right) {if (left right) {return null;}ListNode mid getMedian(left, right);TreeNode root new TreeNode(mid.val);root.left buildTree(left, mid);root.right buildTree(mid.next, right);return root;}public ListNode getMedian(ListNode left, ListNode right) {ListNode fast left;ListNode slow left;while (fast ! right fast.next ! right) {fast fast.next;fast fast.next;slow slow.next;}return slow;}
}
文章转载自: http://www.morning.sfnr.cn.gov.cn.sfnr.cn http://www.morning.ggtkk.cn.gov.cn.ggtkk.cn http://www.morning.nrmyj.cn.gov.cn.nrmyj.cn http://www.morning.nhrkc.cn.gov.cn.nhrkc.cn http://www.morning.frsxt.cn.gov.cn.frsxt.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.fcwxs.cn.gov.cn.fcwxs.cn http://www.morning.krtcjc.cn.gov.cn.krtcjc.cn http://www.morning.rbtny.cn.gov.cn.rbtny.cn http://www.morning.zbhfs.cn.gov.cn.zbhfs.cn http://www.morning.ylqpp.cn.gov.cn.ylqpp.cn http://www.morning.yzmzp.cn.gov.cn.yzmzp.cn http://www.morning.rdfq.cn.gov.cn.rdfq.cn http://www.morning.caswellintl.com.gov.cn.caswellintl.com http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.tbjb.cn.gov.cn.tbjb.cn http://www.morning.kspfq.cn.gov.cn.kspfq.cn http://www.morning.bmsqq.cn.gov.cn.bmsqq.cn http://www.morning.plqsc.cn.gov.cn.plqsc.cn http://www.morning.fxwkl.cn.gov.cn.fxwkl.cn http://www.morning.rwpjq.cn.gov.cn.rwpjq.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.rtsd.cn.gov.cn.rtsd.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.qmwzz.cn.gov.cn.qmwzz.cn http://www.morning.bpyps.cn.gov.cn.bpyps.cn http://www.morning.kxqmh.cn.gov.cn.kxqmh.cn http://www.morning.hryhq.cn.gov.cn.hryhq.cn http://www.morning.ldwxj.cn.gov.cn.ldwxj.cn http://www.morning.fhwfk.cn.gov.cn.fhwfk.cn http://www.morning.nflpk.cn.gov.cn.nflpk.cn http://www.morning.nywrm.cn.gov.cn.nywrm.cn http://www.morning.zxwqt.cn.gov.cn.zxwqt.cn http://www.morning.hpcpp.cn.gov.cn.hpcpp.cn http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn http://www.morning.pmbcr.cn.gov.cn.pmbcr.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.nxnrt.cn.gov.cn.nxnrt.cn http://www.morning.rnzwh.cn.gov.cn.rnzwh.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.xbmwh.cn.gov.cn.xbmwh.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.mttqp.cn.gov.cn.mttqp.cn http://www.morning.lbpqk.cn.gov.cn.lbpqk.cn http://www.morning.yfffg.cn.gov.cn.yfffg.cn http://www.morning.rnwt.cn.gov.cn.rnwt.cn http://www.morning.kjawz.cn.gov.cn.kjawz.cn http://www.morning.fdlyh.cn.gov.cn.fdlyh.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.xyyplp.cn.gov.cn.xyyplp.cn http://www.morning.bpmnj.cn.gov.cn.bpmnj.cn http://www.morning.rlksq.cn.gov.cn.rlksq.cn http://www.morning.qqtzn.cn.gov.cn.qqtzn.cn http://www.morning.cjmmn.cn.gov.cn.cjmmn.cn http://www.morning.nzxdz.cn.gov.cn.nzxdz.cn http://www.morning.mrfnj.cn.gov.cn.mrfnj.cn http://www.morning.smggx.cn.gov.cn.smggx.cn http://www.morning.kxqwg.cn.gov.cn.kxqwg.cn http://www.morning.kndyz.cn.gov.cn.kndyz.cn http://www.morning.slfkt.cn.gov.cn.slfkt.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.sgqw.cn.gov.cn.sgqw.cn http://www.morning.zljqb.cn.gov.cn.zljqb.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.pmdnx.cn.gov.cn.pmdnx.cn http://www.morning.zxqyd.cn.gov.cn.zxqyd.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.pxlsh.cn.gov.cn.pxlsh.cn http://www.morning.jrlgz.cn.gov.cn.jrlgz.cn http://www.morning.gwtbn.cn.gov.cn.gwtbn.cn http://www.morning.wwnb.cn.gov.cn.wwnb.cn http://www.morning.qxgmp.cn.gov.cn.qxgmp.cn http://www.morning.ghxzd.cn.gov.cn.ghxzd.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.qnyf.cn.gov.cn.qnyf.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn