加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

109.Convert Sorted List to Binary Search Tree

发布时间:2020-12-14 03:49:43 所属栏目:大数据 来源:网络整理
导读:109 .?Convert Sorted List to Binary Search Tree(AC) /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ /** * Definition for a binary tree node. * public clas
109.?Convert Sorted List to Binary Search Tree
(AC)

/**
 * 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; }
 * }
 */
  
class Solution {
    public TreeNode sortedListToBST(ListNode head) {
      if(head == null) return null;
      return helper(head,null);
    }
  
    public TreeNode helper(ListNode head,ListNode tail){
      if(head == tail) return null;/////
    
      ListNode slow = head;
      ListNode fast = head;
      
      
      while(fast != tail && fast.next != tail){
        slow = slow.next;
        fast = fast.next.next;
      }
      
      TreeNode root = new TreeNode(slow.val);
      root.left = helper(head,slow);
      root.right = helper(slow.next,tail);
      return root;
    }
}










/// NullPointerException


class Solution {
    public TreeNode sortedListToBST(ListNode head) {
      // base cases 
      // size = 0;
      if(head == null) return null;
      // size = 1
      if(head.next == null){
        TreeNode root = new TreeNode(head.next.val);
        return root;
      }
      
      // size = 2
      if(head.next.next == null){
        TreeNode next = new TreeNode(head.next.val);
        TreeNode root = new TreeNode(head.val);
        
        root.right = next;
        return root;
      }
      
      // size = 3 
      if(head.next.next.next == null){
        TreeNode root = new TreeNode(head.next.val);
        TreeNode right = new TreeNode(head.next.next.val);
        TreeNode left = new TreeNode(head.val);
        
        root.left = left;
        root.right = right;
        
        return root;
      }
      
      // induction rules
      
      ListNode mid = findMid(head);
      ListNode rootNode = mid.next;
      ListNode nextNode = rootNode.next;
      
      mid.next = null;
      rootNode.next = null;
      
      TreeNode root = new TreeNode(rootNode.val);
      root.left = sortedListToBST(head);
      root.right = sortedListToBST(nextNode);
      
      return root;
    }
    
    private ListNode findMid(ListNode head){
      ListNode fast = head;
      ListNode slow = head;
      
      while(fast.next != null && fast.next.next != null){
        fast = fast.next.next;
        slow = slow.next;
      }
      return slow;
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读