109. Convert Sorted List to Binary Search Tree
Posted 鸵鸟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了109. Convert Sorted List to Binary Search Tree相关的知识,希望对你有一定的参考价值。
class Solution { ListNode lnode=null; public TreeNode sortedListToBST(ListNode head) { int size=0; ListNode p=head; while(p!=null) { p=p.next; size++; } lnode=head; return buildBST(size); } private TreeNode buildBST(int size){ if(size==0) return null; TreeNode tnode=new TreeNode(0); tnode.left=buildBST(size/2); tnode.val=lnode.val; lnode=lnode.next; tnode.right=buildBST(size-size/2-1); return tnode; } }
以上是关于109. Convert Sorted List to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章