二叉树-Convert Sorted List to Binary Search Tree
来源:互联网 发布:java 首字母小写 编辑:程序博客网 时间:2024/05/21 21:48
2014.8.18 update:
其实相当简单,就是找到链表的中间node然后左半边成为left node, 右半边成为right node
public class Solution { public TreeNode sortedListToBST(ListNode head) { if (head == null) { return null; } ListNode tail = head; while (tail.next != null) { tail = tail.next; } return helper(head, tail); } TreeNode helper(ListNode head, ListNode tail) { if (head == null || tail == null) { return null; } if (head == tail) { return new TreeNode(head.val); } ListNode fast = head; ListNode slow = head; ListNode preSlow = null; while (fast.next != null && fast != tail) { fast = fast.next; if (fast.next == null || fast == tail) { break; } else { fast = fast.next; } preSlow = slow; slow = slow.next; } TreeNode returnNode = new TreeNode(slow.val); returnNode.left = helper(head, preSlow); returnNode.right = helper(slow.next, tail); return returnNode; }}
2013:
public class Solution {
}
0 0
- 二叉树-Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- convert sorted list to binary search tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- 二叉树-对称树
- ibm的InfoSphere Streams的制备技术指南精通测试V1
- 二叉树-preorder+inorder 与& postorder+inorder
- 二叉树-populate next node
- 二叉树-flatten binary search tree to linked lis
- 二叉树-Convert Sorted List to Binary Search Tree
- Human Gene Functions - POJ 1080 UVaLive 2344 dp
- 二叉树-recover binary search tree
- 罗马数字转化
- Letter combinations of phone number
- sudoku
- word search
- Restore IP address
- Queens