leetcode109 (converted ordered linked list to binary search tree)

Given a singly linked list, the elements in it are sorted in ascending order and converted to a highly balanced binary search tree.
In this question, a highly balanced binary tree means that the absolute value of the height difference between the left and right subtrees of each node of a binary tree does not exceed 1.

Solution (1): Converting an ordered linked list into a binary search tree is actually the process of continuously dicing the ordered linked list into dichotomy. Among them, the node value of the middle linked list node of the left half of the sub-linked list after dichotomy Is the node value of the left node of the current tree node, and the node value of the middle link list node of the right half of the sub-linked list is the node value of the right node of the current tree node. According to the above rules, we only need to continue to divide the linked list

class Solution {
    
    
    TreeNode headNode=new TreeNode();
    public TreeNode sortedListToBST(ListNode head) {
    
    
         if(head==null)
             return null;
         ListNode end=head;
         while(end!=null)
             end=end.next;
         return binaryCreate(head,end);
    }
    //对链表进行二分,同时构造二叉搜索树
    private TreeNode binaryCreate(ListNode start,ListNode end){
    
    
              if(start==end)
                  return null;
              ListNode mid=findMid(start,end);
              TreeNode node=new TreeNode(mid.val);
              node.left=binaryCreate(start,mid);
              node.right=binaryCreate(mid.next,end);
              return node;

    }
    //找到链表的中间结点
    private ListNode findMid(ListNode start,ListNode end){
    
    
            ListNode left=start,right=start;
            while(right!=end&&right.next!=end){
    
    
                right=right.next;
                right=right.next;
                left=left.next;
            }
            return left;
    }
}

Problem solution (2): Inverse construction of middle-order traversal. We know that a binary search tree can be traversed in middle-order to get an ordered array set in ascending order. Similarly, when we have an ordered linked list, we can inversely construct one Binary search tree

class Solution {
    
    
    ListNode headNode;
    public TreeNode sortedListToBST(ListNode head) {
    
    
        headNode=head;
        if(head==null)
            return null;
       ListNode end=head;
       int length=0;
       while(end!=null){
    
    
           end=end.next;
           length++;
       }
       return midDFS(0,length-1);
    }
    private TreeNode midDFS(int start,int end){
    
    
               if(end<start)
                   return null;
               int mid=(start+end)/2;
               TreeNode node=new TreeNode();
               node.left=midDFS(start,mid-1);
               node.val=headNode.val;
               headNode=headNode.next;
               node.right=midDFS(mid+1,end);
               return node;
    }

}

Guess you like

Origin blog.csdn.net/CY2333333/article/details/108080607