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否是空.有的 ...
随机推荐
- 哈尔滨理工大学第七届程序设计竞赛初赛(BFS多队列顺序)
哈尔滨理工大学第七届程序设计竞赛初赛https://www.nowcoder.com/acm/contest/28#question D题wa了半天....(真真正正的半天) 其实D题本来就是一个简单 ...
- calc()语法
什么是calc()? 学习calc()之前,我们有必要先知道calc()是什么?只有知道了他是个什么东东?在实际运用中更好的使用他. calc()从字面我们可以把他理解为一个函数function.其实 ...
- WPF如何用TreeView制作好友列表、播放列表(转)
WPF如何用TreeView制作好友列表.播放列表 前言 TreeView这个控件对于我来说是用得比较多的,以前做的小聊天软件(好友列表).音乐播放器(播放列表).类库展示器(树形类结构)等都用的是T ...
- Web Js推断键盘出发事件
window.document.onkeydown = disableRefresh; function disableRefresh(evt){ evt = (evt) ? evt : wind ...
- VisualSVN安装配置与使用
VisualSVN安装配置与使用 1. 所选服务器安装包:VisualSVN-Server-2.1.3.msi. 2. 客户端安装包:TortoiseSVN-1.6.2.16344-win32-s ...
- TNS-12535 TNS-00505的处理方法
原文地址:TNS-12535 TNS-00505的处理方法 作者:wzq609 硬件说明: 操作系统版本:ORACLE LINUX 6.3 64位 数据库版本:11.2.0.3 64位 问题说明 ...
- Angular 4 路由介绍
Angular 4 路由 1. 创建工程 ng new router --routing 2. 创建home和product组件 ng g component home ng g component ...
- asp.net如何使用cookie(创建、保存、读取)
Cookie的用法也和ASP中差不多.比如我们建立一个名为aspcn,值为大众的cookie HttpCookie cookie = new HttpCookie("aspcn") ...
- PMP学习笔记
PMI:Project Management Institute,项目管理协会于1969年在美国成立,致力于全球范围内的项目管理研究.标准制定和出版.价值倡导.职业认证和学位 课程认证.现在,PMI已 ...
- 关于android webview 设置cookie的问题
转自:http://blog.csdn.net/encienqi/article/details/7912733 我们在android中访问网络经常会用到Apache的HttpClient,用此类去访 ...