关闭

【Leetcode】Convert Sorted List to Binary Search Tree

334人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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.

思路:

简单起见,先把链表值存到数组中,用数组递归感觉会简单点。。

算法:

	public int getListLength(ListNode head) {
		int length = 0;
		ListNode p = head;
		while (p != null) {
			length++;
			p = p.next;
		}
		return length;
	}

	public TreeNode sortedListToBST(ListNode head) {
		ListNode p = head;
		int i = 0;
		int n[] = new int[getListLength(head)];
		while (p != null) {
			n[i] = p.val;
			i++;
			p = p.next;
		}
		return sortedArrayToBST(n);
	}

	/**
	 * Convert Sorted Array to Binary Search Tree
	 */
	public TreeNode sortedArrayToBST(int[] nums) {
		if (nums == null || nums.length == 0) {
			return null;
		}
		return test(nums, 0, nums.length - 1);
	}

	public TreeNode test(int[] nums, int left, int right) {
		if (right < left)
			return null;
		int mid = (left + right) / 2;
		TreeNode root = new TreeNode(nums[mid]);
		root.left = test(nums, left, mid - 1);
		root.right = test(nums, mid + 1, right);
		return root;
	}


0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】

【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  G...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-10 07:46
  • 2750

二叉搜索树(Binary Search Tree )的定义及分析

定义:     二叉搜索树或者是一棵空树,或者是具有下列性质的二叉树: 每个结点都有一个作为搜索依据的关键码(key),所有结点的关键码互不相同。 左子树(如果非空)上所...
  • jsh13417
  • jsh13417
  • 2013-01-22 21:59
  • 3322

【LeetCode-面试算法经典-Java实现】【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】

【103-Binary Tree Zigzag Level Order Traversal(二叉树分层Z字形遍历)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Give...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-08 06:35
  • 2568

LeetCode- 108/109. Convert Sorted Array/List to Binary Search Tree (JAVA)

LeetCode- 108/109. Convert Sorted Array/List to Binary Search Tree (JAVA),109. Convert Sorted List ...
  • mine_song
  • mine_song
  • 2017-04-13 00:09
  • 216

LeetCode(109)Convert Sorted List to Binary Search Tree

题目如下: Given a singly linked list where elements are sorted in ascending order, convert it to a heigh...
  • feliciafay
  • feliciafay
  • 2014-01-19 10:53
  • 2343

LeetCode109 Convert Sorted List to Binary Search Tree

详细见:leetcode.com/problems/convert-sorted-list-to-binary-search-tree Java Solution: github pa...
  • zxwtry
  • zxwtry
  • 2017-04-29 18:55
  • 120

LeetCode | Convert Sorted List to Binary Search Tree(链表转换成二叉搜索树)

Given a singly linked list where elements are sorted in ascending order, convert it to a height bala...
  • a45872055555
  • a45872055555
  • 2014-08-14 16:11
  • 482

<LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree

Total Accepted: 71642 Total Submissions: 233074 Difficulty: Medium Given a singly linked list w...
  • EbowTang
  • EbowTang
  • 2016-06-02 21:48
  • 744

【LeetCode】Convert Sorted List to Binary Search Tree

题目:Convert Sorted List to Binary Search Tree /**LeetCode convert link-list to Binary Search T...
  • u011955072
  • u011955072
  • 2015-03-28 23:55
  • 502

LeetCode 109. Convert Sorted List to Binary Search Tree(链表到二叉搜索树)

原题网址:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Given a singly linked ...
  • jmspan
  • jmspan
  • 2016-05-24 00:12
  • 406
    个人资料
    • 访问:555973次
    • 积分:8554
    • 等级:
    • 排名:第2653名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论