
递归
wutingyehe
Just for fun
展开
-
LintCode 二叉树的最小深度
给定一个二叉树,找出其最小深度。 二叉树的最小深度为根节点到最近叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最小深度为 2Given a binary tree, find its minimum depth. The minimum depth is the number of n原创 2015-06-26 10:33:21 · 1125 阅读 · 0 评论 -
LinCode Maximum Depth of Binary Tree 二叉树的最大深度
中文版: 给定一个二叉树,找出其最大深度。 二叉树的深度为根节点到最远叶子节点的距离。样例 给出一棵如下的二叉树: 1 / \ 2 3 / \ 4 5 这个二叉树的最大深度为3.English Version: Given a binary tree, find its maximum depth. The maximum depth is the n原创 2015-07-11 21:40:59 · 506 阅读 · 0 评论 -
LintCode Climbing Stairs 爬楼梯
中文描述: 假设你正在爬楼梯,需要n步你才能到达顶部。但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,1+1+1=1+2=2+1=3,共有3中不同的方法返回 3English Version: You are climbing a stair case. It takes n steps to reach to the top. Each time you原创 2015-07-11 22:05:58 · 1704 阅读 · 0 评论 -
[LintCode]Segment Tree Query 线段树的查询
For an integer array (index from 0 to n-1, where n is the size of this array), in the corresponding SegmentTree, each node stores an extra attribute max to denote the maximum number in the interval of原创 2015-09-06 15:01:40 · 901 阅读 · 0 评论 -
[LintCode] 将二叉查找树转换成双链表 Convert Binary Search Tree to Doubly Linked List
将一个二叉查找树按照中序遍历转换成双向链表。 样例 给定一个二叉查找树: 4 / \ 2 5 / \ 1 3 返回 1<->2<->3<->4<->5。Convert a binary search tree to doubly linked list with in-order traversal. Example Given a binary se原创 2016-04-25 22:26:32 · 2423 阅读 · 0 评论 -
[LintCode] 第K大元素 Kth Largest Element
在数组中找到第k大的元素。 样例 给出数组 [9,3,2,4,8],第三大的元素是 4 给出数组 [1,2,3,4,5],第一大的元素是 5,第二大的元素是 4,第三大的元素是 3,以此类推 挑战 要求时间复杂度为O(n),空间复杂度为O(1)Find K-th largest element in an array. Example In array [9,3,2,4,8], th原创 2016-04-26 15:57:32 · 1146 阅读 · 1 评论 -
[LintCode] 中序遍历和后序遍历树构造二叉树 Construct Binary Tree from Inorder and Postorder Traversal
根据中序遍历和后序遍历树构造二叉树 注意事项 你可以假设树中不存在相同数值的节点 样例 给出树的中序遍历: [1,2,3] 和后序遍历: [1,3,2] 返回如下的树: 2 / \ 1 3Given inorder and postorder traversal of a tree, construct the binary tree. Notice You may原创 2016-04-18 20:25:03 · 701 阅读 · 0 评论 -
[LintCode] 用递归打印数字 Print Numbers by Recursion
用递归的方法找到从1到最大的N位整数。你能够用深度最多只有 N 层的递归么?样例 给出 N = 1, 返回[1,2,3,4,5,6,7,8,9]. 给出 N = 2, 返回[1,2,3,4,5,6,7,8,9,10,11,…,99].Print numbers from 1 to the largest number with N digits by recursion. Can you re原创 2016-04-19 16:14:22 · 980 阅读 · 0 评论 -
[LintCode] 二叉树的路径之和 Binary Tree Path Sum
给定一个二叉树,找出所有路径中各节点相加总和等于给定 目标值 的路径。 一个有效的路径,指的是从根节点到叶节点的路径。样例 给定一个二叉树,和 目标值 = 5: 1 / \ 2 4 / \ 2 3 返回: [ [1, 2, 2], [1, 4] ]Given a binary tree, find all paths that s原创 2016-04-22 09:38:38 · 3909 阅读 · 1 评论