Convert Sorted List to Binary Search Tree java

 1 public TreeNode sortedListToBST(ListNode head) {
 2          if(head==null) return new TreeNode(0);
 3             ArrayList<TreeNode> arr=new  ArrayList<TreeNode>();
 4             while(head!=null)
 5             {
 6                 arr.add(new TreeNode(head.val));
 7                 head=head.next;
 8             }
 9
10            return  BST(arr,0,arr.size()-1);
11         }
12      TreeNode BST(ArrayList<TreeNode> list,int start,int end)
13      {
14          if(start>=end)
15          {
16              return list.get(start);
17          }
18          else
19          {
20              int mid=start+(end-start)/2;
21              TreeNode root=list.get(mid);
22              root.left=BST(list,start,mid-1);
23              root.right=BST(list,mid+1,end);
24
25              return root;
26          }
27      }
时间: 2024-10-01 07:16:28

Convert Sorted List to Binary Search Tree java的相关文章

leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一个排好序的数组,然后求搜索二叉树 其实就是二分法,不难. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNo

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https:

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree &amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**