【Leetcode】Two Problems: Convert Sorted Array/List to Binary Search Tree
2016-02-08 07:56
483 查看
First let's try easier one:
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
The logic is very simple: Every time you find the middle element and put it as the root and recursively use the function for root.left
and root.right.
That's why we need a helper function to implement this.
Then we advanced this problem as:
Given
a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Easiest way is to convert the unknown problem into a known problem.
Convert the LinkedList into Array:
Do remember, never try to use iterative on tree
Must be some way of using recursive.
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
The logic is very simple: Every time you find the middle element and put it as the root and recursively use the function for root.left
and root.right.
That's why we need a helper function to implement this.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode sortedArrayToBST(int[] nums) { if(nums==null||nums.length==0) return null; return helper(nums,0,nums.length-1); } public TreeNode helper(int[] nums, int begin, int end){ if(begin > end) return null;//base case int mid = (begin+end)/2; TreeNode root = new TreeNode(nums[mid]); root.left = helper(nums,begin, mid-1); root.right = helper(nums, mid+1,end); return root; } }
Then we advanced this problem as:
Given
a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Easiest way is to convert the unknown problem into a known problem.
Convert the LinkedList into Array:
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode sortedListToBST(ListNode head) { if(head==null) return null; List<Integer> list = new ArrayList<Integer>(); while(head!=null){ list.add(head.val); head = head.next; } int[] ret = new int[list.size()]; for (int i=0; i < ret.length; i++) { ret[i] = list.get(i).intValue(); } return helper(ret,0,ret.length-1); } public TreeNode helper(int[] nums, int begin, int end){ if(begin>end) return null; int mid = (begin+end)/2; TreeNode root = new TreeNode(nums[mid]); root.left = helper(nums,begin, mid-1); root.right = helper(nums,mid+1,end); return root; } }
Do remember, never try to use iterative on tree
Must be some way of using recursive.
相关文章推荐
- 关于RHEL6.x上avahi进程与Dell的Fluid Cache
- git常用命令小记
- GeeksforGeeks 统计一个数组中的相反的数的对数
- 2016年网络SEO外链指南
- 阿里云linux CentOS6.5(nginx+PHP-fpm)及RDS初级使用指南和简单安全设置
- 你知道学英语有什么好处么……
- [LeetCode] Missing Ranges 缺失区间
- GeeksForGeeks Power(x,n)
- jquery操作select(取值,设置选中)
- js一点代码备用
- 【Leetcode】Maximum Depth of Binary Tree
- 正则实现文本框只能输入16个数字,每4位数字后添加一个空格
- 造轮子:js限制文本框只能输入数字方法
- 入学一月谈
- 入学一月谈
- 05-mac下SecureCRT安装
- php面向对象之——类的定义和属性方法的调用
- Kinect For Windows V2开发日志五:使用OpenCV显示彩色图像及红外图像
- Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案
- Atitit.跨语言数据库db api兼容性 jdbc odbc ado oledb 增强方案