Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 分析: 这题并不难.其实就是深搜. 就…
摘自:http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html 4.在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树. 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径. 打印出和与输入整数相等的所有路径. 例如输入整数22 和如下二元树 10 / \ 5 12 /\ 4 7 则打印出两条路径:10, 12 和10, 5, 7. 二元树节点的数据结构定义为: struct BinaryTreeNode…
算法!用C#写一个函数,在一个数组中找出随意几个值相加等于一个值比如,数组{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}  要找出那些数相加等于100 解决方案一: #region 解决方案一 , , , , , , , , , , , , , , , , , , , }; List<List<int>> mylist = new List<List<int>>(); int length = mya…
Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target. Note: Given target value is a floating point. You are guaranteed to have only one unique value in the BST that is closest to the target.…
      二叉搜索树中第K小的元素     给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 示例 1: 输入: root = [3,1,4,null,2], k = 1 3 / \ 1 4 \   2 输出: 1 示例 2: 输入: root = [5,3,6,2,4,null,null,1], k = 3 5 / \ 3 6 / \ 2 4 / 1 输出: 3 进阶:如果二叉搜…
问题: Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?   分析: 数组中的数除了一个只出现了一次之外,其它都出现了两次, 要找出只出…
上一次,我用TF-IDF算法自动提取关键词. 今天,我们再来研究另一个相关的问题.有些时候,除了找到关键词,我们还希望找到与原文章相似的其他文章.比如,"Google新闻"在主新闻下方,还提供多条相似的新闻. 为了找出相似的文章,需要用到"余弦相似性"(cosine similiarity).下面,我举一个例子来说明,什么是"余弦相似性". 为了简单起见,我们先从句子着手. 句子A:我喜欢看电视,不喜欢看电影. 句子B:我不喜欢看电视,也不喜欢看…
给你一个矩阵 mat,其中每一行的元素都已经按 递增 顺序排好了.请你帮忙找出在所有这些行中 最小的公共元素. 如果矩阵中没有这样的公共元素,就请返回 -1. 示例: 输入:mat = [[,,,,],[,,,,],[,,,,],[,,,,]] 输出: 解法: 暴力解法  就是使用哈希记录每行 然后比较 代码 class Solution { public: unordered_map<]; int smallestCommonElement(vector<vector<int>&…
题目: 给定一个旋转数组,但是你不知道旋转位置,在旋转数组中找出给定target值出现的位置:你可以假设在数组中没有重复值出现 举例: (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). 在旋转后的数组中查找6,则返回index =2. 若查找10,则返回index= -1: 解题思路: 由于数组中没有重复出现的数字,因此旋转后的数组基本可以划分为两部分:因此解题思路可以转换为我们熟悉的二分查找:但是在二分查找时我们需要注意,到底在哪边查找? 1)…
Given a sorted integer array where the range of elements are in the inclusive range [lower, upper], return its missing ranges. For example, given [0, 1, 3, 50, 75], lower = 0 and upper = 99, return ["2", "4->49", "51->74&quo…