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否是空.有的 ...
随机推荐
- Microsoft - Union Two Sorted List with Distinct Value
Union Two Sorted List with Distinct Value Given X = { 10, 12, 16, 20 } & Y = {12, 18, 20, 22} W ...
- Ubuntu 16.04下指定Sublime Text 3 默认python编译版本
安装PackageResourceViewer插件 输入 Ctrl+Shift+P 输入install,选择Package Control: Install Package 选择PackageReso ...
- test20181015 B君的第二题
题意 分析 考场85分 用multiset暴力,由于教练的机子飞快,有写priority_queue水过了的人. #include<cstdlib> #include<cstdio& ...
- UVA10590 Boxes of Chocolates Again
题意 将正整数N拆分成若干个正整数之和,问有多少种不重复的拆分方案. \(n \leq 5000\) 分析 用f(i,j)表示将i拆成若干个数字,最大的那个数字(即最后一个数)不超过j的方案数. 转移 ...
- Select2 用法
http://www.cnblogs.com/wuhuacong/p/4761637.html 2.这个做详细参考 http://www.jianshu.com/p/c5ab74b91b2e 3.ht ...
- ExtJs 4.0 DeskTop集成 百度地图API
经过3天的奋斗最终搞了出来, 网上的资料非常少,希望小⑦的文章对读者有点帮助,PS:小⑦非常努力的~. 不废话,上代码了. 首先.去百度官网Copy一个模版 http://api.map.baidu. ...
- flash流媒体资料
http://www.docin.com/p-111227070.html Java mina和c++ ace做socket长连接 测试报告: http://www.iteye.com/problem ...
- ElementUI 知识点
类型是number的el-input 去掉滚轮事件: @mousewheel.native.prevent <el-input type="number" @mousewhe ...
- php设计模式:单例模式
前些日子开始着真正的去了解下设计模式,开始么,简单地从单例模式开始,当然网上看了一些资料,单例模式比较好理解,看看介绍,然后看看代码基本也就能够理解了,设计模式这些的花点心思基本的是能够理解的,当然要 ...
- tyvj1391走廊泼水节
题目:http://www.joyoi.cn/problem/tyvj-1391 考虑对于每一条边,新加的长度是(左边点数*右边点数-1)*(当前边权+1): 然后要kruskal的思想,把边从小到大 ...