108. Convert Sorted Array to Binary Search Tree (building tree with resursion)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted array: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0
/ \
-3 9
/ /
-10 5
Solution: O(n) , space: 栈空间O(logn)(from recusrsive expression)加上结果的空间O(n) : O(n) (good reference: https://blog.csdn.net/linhuanmars/article/details/23904883)
- sorting array for BST(left < root < right)
- start from middle node and let left part as left subtree , right as well
- recursion with returing root-- pattern:
TreeNode root = new TreeNode(nums[m]);
root.left = helper(nums, l, m-1);
root.right = helper(nums, m+1, r);
return root;
Totally
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution { public TreeNode sortedArrayToBST(int[] nums) {
if(nums.length == 0) return null;
return helper(nums, 0, nums.length-1);
}
// //recursive with return,
TreeNode helper(int[] nums, int l, int r){
if(l > r) return null;
int m = (r-l)/2 + l;
TreeNode root = new TreeNode(nums[m]);
root.left = helper(nums, l, m-1);
root.right = helper(nums, m+1, r);
return root;
}
}
Follow up questions: 109 convert sorted list to BST
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Example: Given the sorted linked list: [-10,-3,0,5,9], One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 0
/ \
-3 9
/ /
-10 5
Solution:
/**
* 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; }
* }
*/
class Solution {
//one way: get middle of linkedlist (slow, fast)
//another way: use preorder(left, root, right), need get the number of anodes in the list
//1: int m = (r-l)/2 + l; 2: //node just copy the reference
public TreeNode sortedListToBST(ListNode head) {
if(head == null) return null;
ListNode cur = head;
int m = 0;
while(cur != null){
m++;
cur = cur.next;
}
List<ListNode> list = new ArrayList<>();
list.add(head);
return helper(list, 0, m-1);
}
TreeNode helper(List<ListNode> list, int l, int r){ //node just copy the reference
if(l>r) return null; int m = (r-l)/2 + l;
TreeNode left = helper(list, l, m-1);//
TreeNode root = new TreeNode(list.get(0).val);
root.left = left;
list.set(0, list.get(0).next);
root.right = helper(list, m+1, r);
return root;
} }
Solution 2: get middle of list (slow and fast)
/**
* 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;
return BST(head, null);
}
public TreeNode BST(ListNode head, ListNode tail) {
if(head == tail) return null; ListNode slow = head;
ListNode fast = head;
while(fast!=tail&&fast.next!=tail) { //tail
fast = fast.next.next;
slow = slow.next;
}
TreeNode node = new TreeNode(slow.val);
node.left = BST(head, slow);
node.right = BST(slow.next, tail);
return node;
}
}
108. Convert Sorted Array to Binary Search Tree (building tree with resursion)的更多相关文章
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...
- 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree (将有序数组转换成BST)
108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...
- leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...
- 108. Convert Sorted Array to Binary Search [Python]
108. Convert Sorted Array to Binary Search Given an array where elements are sorted in ascending ord ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree 把有序数组转成二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目 ...
- Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)
1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...
- [LeetCode&Python] Problem 108. Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Fo ...
随机推荐
- 小a与星际探索---DP
题目描述 小a正在玩一款星际探索游戏,小a需要驾驶着飞船从11号星球出发前往nn号星球.其中每个星球有一个能量指数pp.星球ii能到达星球jj当且仅当pi>pjpi>pj.同时小a的飞船还 ...
- nginx+uwsgi+virtualenv+supervisor部署项目
一.导论 WSGI是Web服务器网关接口.它是一个规范,描述了Web服务器如何与Web应用程序通信,以及Web应用程序如何链接在一起以处理一个请求,(接收请求,处理请求,响应请求) 基于wsgi运行的 ...
- aoj0118
一.题意:有三种水果分别用,'@','*','#'三种符号表示,上下左右相连的同种水果被看做是一个区域,问一共有多少个区域 二.思路:用dfs去标记相连区域,然后遍历每个没有被标记的位置进行dfs 三 ...
- python小商店
(1) 输入自己所有的钱.(2) 展示商品的序号,名称及其价格.(3) 输入要买商品的序号.(4) 输入要买商品的数量.(5) 购物车中显示购买的水果名称及其对应的数量和剩余钱.(6) 如果序号输入有 ...
- 利用paramiko的demo_simple.py进行日志记录时遇到的特殊字符
特殊字符列表: 回车 "\r" "\x13" 响铃 "\x07" 换行 "\n" "\x10" &q ...
- vue中promise的使用
vue中promise的使用 promise是处理异步的利器,在之前的文章<ES6之promise>中,我详细介绍了promise的使用, 在文章<js动画实现&&回 ...
- Oracle 数据库和Sql Server数据库的区别
Oracle数据库的访问方式,和SqlServer数据库是有很大差别的,下面用图来说明: 1.Sql Server数据库 SqlServer数据库的访问方式,大致是:假设用户通过sa登录SqlServ ...
- JDK7之HashMap源码
并发场景下使用HashMap的问题分析:疫苗:Java HashMap的死循环 http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6423457 ...
- unity摄像机脚本
直接挂载在摄像机上面即可 1.摄像机自由平移 using UnityEngine; using System.Collections; /// <summary> /// 摄像机视角自由移 ...
- [linux]解决DNS配置重启丢失
DNS配置重启丢失 每次重启后都修改DNS配置文件 /etc/resolv.conf从网上得知 /etc/resolv.conf中的DNS配置是从/etc/resolvconf/resolv.conf ...