
算法题
文章平均质量分 72
lxs1995
这个作者很懒,什么都没留下…
展开
-
算法题:输出单层节点
题目:对于一棵二叉树,请设计一个算法,创建含有某一深度上所有结点的链表。给定二叉树的根结点指针TreeNode* root,以及链表上结点的深度,请返回一个链表ListNode,代表该深度上所有结点的值,请按树上从左往右的顺序链接,保证深度不超过树的高度,树上结点的值为非负整数且不超过100000。解答:方法1:层次遍历import java.util.*; public class TreeLe...原创 2018-03-07 17:38:06 · 228 阅读 · 0 评论 -
Leetcode 200:Number of Island
题目:Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may ass...原创 2018-03-19 10:46:12 · 506 阅读 · 0 评论 -
递归算法学习——Path Sum系列
一、递归递归很重要的两个条件:1)确定终止条件;2)递归的过程。两个条件都需要细致地分析问题的细节。二、算法题1、Path Sum ,LeetCode第112题(注意递归的终止条件)问题描述:给出一棵二叉树以及一个数字sum,判断在这棵二叉树上是否存在一条从根到叶子的路径,其路径上所有节点为sumclass Solution { public boolean hasPathSum(Tree...原创 2018-04-08 17:01:43 · 229 阅读 · 0 评论 -
LeetCode-114:Flatten Binary Tree to Linked List
1、第一种方法,使用递归方法:据展开后形成的链表的顺序分析出是使用先序遍历,那么只要是数的遍历就有递归和非递归的两种方法来求解,这里我们也用两种方法来求解。首先来看递归版本的,思路是先利用DFS的思路找到最左子节点,然后回到其父节点,把其父节点和右子节点断开,将原左子结点连上父节点的右子节点上,然后再把原右子节点连到新右子节点的右子节点上,然后再回到上一父节点做相同操作。class Solutio...原创 2018-03-26 15:50:41 · 156 阅读 · 0 评论 -
Leetcode 105 Construct Binary Tree from Preorder and Inorder Traversal
一、题目 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given preorder = [3,9,20,15,7] inorder ...原创 2018-07-17 21:18:23 · 161 阅读 · 0 评论 -
LeetCode——376. Wiggle Subsequence
题目: A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be eithe...原创 2018-07-17 21:18:50 · 156 阅读 · 0 评论 -
LeetCode------72. Edit Distance
一、题目 Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a...原创 2018-08-14 20:33:06 · 145 阅读 · 0 评论