leetcode108
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
//private TreeNode Insert(TreeNode T, int x)
//{
// if (T == null)
// {
// T = new TreeNode(x);
// return T;
// }
// else if (x < T.val)
// {
// T.left = Insert(T.left, x);
// }
// else if (x > T.val)
// {
// T.right = Insert(T.right, x);
// }
// return T;
//} Queue<TreeNode> Q = new Queue<TreeNode>(); List<TreeNode> TreeList = new List<TreeNode>(); void InOrder(TreeNode node)
{
if (node != null)
{
if (node.left != null)
{
InOrder(node.left);
} //中序处理
TreeList.Add(node); if (node.right != null)
{
InOrder(node.right);
}
}
} TreeNode BFS(int count)
{
int index = ;
TreeNode root = null;
while (Q.Count > && index < count)
{
var n = Q.Dequeue();
if (n == null)
{
index++;
n = new TreeNode(index);
root = n;
Q.Enqueue(n);
}
else
{
if (n.left == null && index < count)
{
index++;
n.left = new TreeNode(index); Q.Enqueue(n.left);
}
if (n.right == null && index < count)
{
index++;
n.right = new TreeNode(index);
Q.Enqueue(n.right);
}
}
}
return root;
} public TreeNode SortedArrayToBST(int[] nums)
{
var count = nums.Length;
Q.Enqueue(null);
var root = BFS(count); InOrder(root); for (int i = ; i < TreeList.Count; i++)
{
TreeList[i].val = nums[i];
} return root;
}
}
https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description
补充一个python的实现,使用递归处理:
class Solution:
def sortedArrayToBST(self, nums: List[int]) -> TreeNode:
n = len(nums)
if n == :#终止条件,返回空
return None
if n == :#终止条件,返回单节点
return TreeNode(nums[])
mid = n //
root = TreeNode(nums[mid])#将数组从中间一分为二
root.left = self.sortedArrayToBST(nums[:mid])#左子树
root.right = self.sortedArrayToBST(nums[mid+:])#右子树
return root#返回
leetcode108的更多相关文章
- [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | 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,109] 将有序数组转换为二叉搜索树
109. 有序链表转换二叉搜索树 Given a singly linked list where elements are sorted in ascending order, convert it ...
- LeetCode108.将有序数组转换为二叉搜索树
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- LeetCode108——Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- LeetCode108 Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. (M ...
- LeetCode108 将有序数组转为二叉搜索树
将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定有序数组: [-10,-3,0, ...
- LeetCode108.有序数组转二叉搜索树
题目 1 class Solution { 2 public: 3 TreeNode* sortedArrayToBST(vector<int>& nums) { 4 if(num ...
- c++算法实现(一) - 递归和初始化
递归 写递归函数经常出错,提醒自己两个规则: 1. 一般来说递归函数中不应该出现for.while之类的循环语句, 因为递归就是循环的另外一种实现: 2. 注意基线条件,具体参考<算法图解> ...
- leetcode_二叉树篇_python
主要是深度遍历和层序遍历的递归和迭代写法. 另外注意:因为求深度可以从上到下去查 所以需要前序遍历(中左右),而高度只能从下到上去查,所以只能后序遍历(左右中). 所有题目首先考虑root否是空.有的 ...
随机推荐
- JavaWeb学习总结(二)-修改Tomcat服务器的端口(半年之后再总结)
一.Tomcat服务器端口的配置 Tomcat的所有配置都放在conf文件夹之中,里面的server.xml文件是配置的核心文件(hibernate.cfg.xml是核心配置文件). 如果想修改Tom ...
- ballerina 学习 三十一 扩展开发(二)
上篇说了使用ballerina 语言开发扩展模块,对于注解类型的我们是需要使用java 语言进行 开发的 官方提供了一个hello 的demo可以参考 https://github.com/balle ...
- spring initializr 创建项目时,依赖对应的 artifactId
选择各个依赖时,对应的 artifactId 选择的依赖 artifactId名字 不选择依赖,默认包含的artifactId spring-boot-starter.spring-boot-star ...
- jquery编辑插件tinyMCE的使用方法
jquery编辑插件tinyMCE是一个非常容易集成到您系统的一个html编辑插件,它不像FckEditor那样需要针对专门的后台语言集成,tinyMCE既能做到轻松集成asp.net,php,jav ...
- 织梦if标签
{dede:field name='id' runphp='yes' } if(@me < 100 && @me >94) @me='xxxxxx';else @me='y ...
- ASP.NET ASHX中获得Session
有时候需要在ASHX中获取Session,可是一般是获取不到的,如何解决? 1-在 aspx和aspx.cs中,都是以Session["xxx"]="aaa"和 ...
- ubuntu 安装时分辨率太小 导致无法继续安装
当分辨率是800 *600时,底部的按钮无法显示,不能继续安装. 可以在右上角,点击电源按钮,在系统设置中调整显示的分辨率后,继续安装.
- SpringBoot中实现依赖注入功能
本文转载自:https://blog.csdn.net/linzhiqiang0316/article/details/52639888 今天给大家介绍一下SpringBoot中是如何实现依赖注入的功 ...
- 【Spring学习笔记-MVC-16】Spring MVC之重定向-解决中文乱码
概述 spring MVC框架controller间跳转,需重定向,主要有如下三种: 不带参数跳转:形如:http://localhost:8080/SpringMVCTest/test/myRedi ...
- R语言学习——欧拉计划(11)Largest product in a grid
Problem 11 In the 20×20 grid below, four numbers along a diagonal line have been marked in red. 08 0 ...