zoukankan      html  css  js  c++  java
  • leetcode 109 Convert Sorted List to Binary Search Tree ----- java

    Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

     
    和上一题类似,把数组换成链表,所以可以两种做法:
    1、把链表换成数组,然后用上一题的方法,这样会比较慢。
    2、每次找到中间的点,作为节点,然后递归,其实原理还是二分查找。
     
    /**
     * 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) {
            List list = new ArrayList<Integer>();
            if( head == null)
                return null;
            return helper(head,null);
        }
        public TreeNode helper(ListNode head,ListNode target){
    
            if( head == target )
                return null;
            ListNode node1 = head;
            ListNode node2 = head;
    
            while( node2 != target && node2.next != target){
                node1 = node1.next;
                node2 = node2.next.next;
            }
            TreeNode node = new TreeNode(node1.val);
    
            node.left = helper(head,node1);
            node.right = helper(node1.next,target);
    
            return node;
    
    
    
            }
    }

    第一种:

    /**
     * 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) {
            List list = new ArrayList<Integer>();
            if( head == null)
                return null;
            while( head != null ){
                list.add(head.val);
                head = head.next;
            }
            int[] nums = new int[list.size()];
            for( int i = 0;i<list.size();i++)
                nums[i] = (int) list.get(i);
            return sortedArrayToBST(nums);
        }
        public TreeNode sortedArrayToBST(int[] nums) {
                int len = nums.length;
                return helper(nums,0,(len-1)/2,len-1);
    
            }
    
            public TreeNode helper(int[] nums,int start,int mid,int end){
                
                if( start > end )
                    return null;
                TreeNode node = new TreeNode(nums[mid]);
    
                node.left = helper(nums,start,(mid+start-1)/2,mid-1);
    
                node.right = helper(nums,mid+1,(end+mid+1)/2,end);
    
                return node;
    
            }
    }
  • 相关阅读:
    iphone, iphone4, ipad 图标和背景图片问题(转)
    ios项目icon和default图片命名规则 (转)
    ios判断设备是iphone还是ipad
    cocos2d学习(一)helloWorld
    判断设备是否是 iphone5
    字节对齐(转)
    NSArray排序
    C++复习之运算符重载,数组排序,vector
    socket编程(转)
    win32下的socket编程
  • 原文地址:https://www.cnblogs.com/xiaoba1203/p/6011136.html
Copyright © 2011-2022 走看看