109. Convert Sorted List to Binary Search Tree
Posted 咖啡中不塌缩的方糖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了109. Convert Sorted List to Binary Search Tree相关的知识,希望对你有一定的参考价值。
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
BST
public TreeNode SortedListToBST(ListNode head) { TreeNode tree = null; if(head == null) return tree; if(head.next == null) return new TreeNode(head.val); //find the middle pionter; var walker = head; var runner = head; var prev = new ListNode(-1); while(runner != null && runner.next != null) { prev = walker; walker = walker.next; runner = runner.next.next; } prev.next =null; //root tree = new TreeNode(walker.val); //subtree tree.left = SortedListToBST(head); tree.right = SortedListToBST(walker.next); return tree; }
以上是关于109. Convert Sorted List to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章
109. Convert Sorted List to Binary Search Tree
109. Convert Sorted List to Binary Search Tree
109. Convert Sorted List to Binary Search Tree
109. Convert Sorted List to Binary Search Tree