LeetCode Convert Sorted List to Binary Search Tree
来源:互联网 发布:数据库原理教学大纲 编辑:程序博客网 时间:2024/06/04 18:25
Convert Sorted List to Binary Search Tree
Solution:
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
if (head == null) return null;
if (head.next == null) return new TreeNode(head.val);
ListNode headcopy = head;
ListNode next = head;
ListNode leftend = head;
while (next.next.next != null){
next = next.next.next;
leftend = headcopy;
headcopy = headcopy.next;
if (next.next == null) break;
}
ListNode right = headcopy.next;
leftend.next = null;
TreeNode root = new TreeNode(headcopy.val);
if (headcopy != head) root.left = sortedListToBST(head); else root.left = null;
root.right = sortedListToBST(right);
return root;
}
}
0 0
- 【LeetCode】Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
- LeetCode[Tree]: Convert Sorted List to Binary Search Tree
- [leetcode][list][dfs] Convert Sorted List to Binary Search Tree
- LeetCode: Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- LeetCode : Convert Sorted List to Binary Search Tree
- [Leetcode] Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Balanced Binary Search Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- leetcode 66: Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode:Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- [Leetcode]Convert Sorted List to Binary Search Tree
- [leetcode]Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Binary Search Tree
- linux 压缩指令及名称意义统计
- 1032 1032. Sharing (25)(自己写的超时了,参考的方法好。用标记来看是否已经检查过这个点)
- 寻找倒数第K个节点...
- 3gpp plmn document
- 网银u盾工作原理
- LeetCode Convert Sorted List to Binary Search Tree
- ios数据解析篇
- Android mediaplayer
- Activiti学习笔记
- DOM 小结
- gcc创建,使用静态库
- hudoj1001——Sum Problem
- POJ 2352 Stars
- 敏捷开发之伤——站会