- 博客(59)
- 收藏
- 关注
原创 Leetcode Path Sum
题目:Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary
2015-05-14 22:13:41 326
原创 Leetcode Sum Root to Leaf Numbers
题目:Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.An example is the root-to-leaf path 1->2->3 which represents the number 123.
2015-05-14 21:55:52 404
原创 Leetcode Binary Tree Right Side View
题目:Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example:Given the following binary
2015-05-14 11:32:03 292
原创 Leetcode Binary Search Tree Iterator 二叉搜索树迭代器
题目:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.
2015-05-14 11:18:19 351
原创 Leetcode Populating Next Right Pointers in Each Node II
题目:Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only
2015-05-14 10:53:12 252
原创 Leetcode Populating Next Right Pointers in Each Node
题目:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next
2015-05-14 10:41:48 292
原创 Leetcode Minimum Depth of Binary Tree 二叉树最小深度
题目:Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.分析:这道题可以和求最大深度一
2015-05-13 17:43:17 328
原创 Leetcode Construct Binary Tree from Inorder and Postorder Traversal 中序后序遍历重组二叉树
题目:Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.分析:1. 后序遍历的最后一个节点为树的根节点2. 找到根节点在中
2015-05-13 17:00:21 453
原创 Leetcode 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.分析:1. 前序遍历的第一个节点为树的根节点2. 在中序遍历中找到根
2015-05-13 16:29:44 438
原创 Leetcode Single Number
题目:Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear run time complexity. Could you implement it wit
2015-05-12 18:19:01 257
原创 Leetcode Balanced Binary Tree 平衡二叉树
题目:Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every nod
2015-05-12 14:55:22 333
原创 Leetcode Convert Sorted Array to Binary Search Tree 有序数组转换成二叉搜索树BST
题目:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.分析:这道题和有序链表转换成BST的方法相同,都是用递归。 1. 递归的结束条件是当start>=end2. 找到数组的中点,作为根节点3. 递归的在左半边构建
2015-05-12 12:20:47 539 1
原创 Leetcode Triangle
题目:Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2],
2015-05-12 11:34:33 267
原创 Leetcode Unique Paths II
题目:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectiv
2015-05-11 22:58:51 258
原创 Leetcode Unique Paths
题目:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying
2015-05-11 22:42:17 250
原创 Leetcode Best Time to Buy and Sell Stock
题目:Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share o
2015-05-11 22:32:59 313
原创 Leetcode House Robber
题目:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that a
2015-05-11 20:49:59 285
原创 Leetcode Climbing Stairs 爬楼梯
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?分析:这道题属于动
2015-05-11 20:34:50 322
原创 动态规划题目思路
做动态规划题目主要考虑的几个方面:1. 每一个状态的含义,其中蕴含了怎样的小规模问题的结果2. 状态之间的关系,即如何通过小规模的状态计算大规模的状态3. 初始化状态,首先找到最小的问题的状态3. 结果,哪一个状态是最终需要的结果什么情况下想到用动态规划:1. 问题需要求最大/最小2. 求是还是不是3. 找到所有可能的方案4. 不能用排序
2015-05-11 20:24:16 294
原创 Leetcode Binary Tree Postorder Traversal 二叉树后序遍历
题目:Given a binary tree, return the postorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [3,2,1].Note: Re
2015-05-11 19:48:03 283
原创 Leetcode Symmetric Tree 对称二叉树
题目:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3
2015-05-11 19:10:13 291
原创 Leetcode Binary Tree Inorder Traversal 二叉树中序遍历
题目:Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recu
2015-05-11 18:54:48 256
原创 Leetcode Binary Tree Preorder Traversal 二叉树先序遍历
题目:Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Rec
2015-05-11 17:18:54 310
原创 Leetcode Binary Tree Zigzag Level Order Traversal
题目:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example:
2015-05-11 15:13:49 260
原创 Leetcode Binary Tree Level Order Traversal II 二叉树分层遍历
题目:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,2
2015-05-11 14:16:41 257
原创 Leetcode Binary Tree Level Order Traversal 二叉树分层遍历
题目:Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \
2015-05-11 14:07:00 298
原创 Leetcode Maximum Depth of Binary Tree 二叉树最大深度
题目:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.分析:递归的向下找, 取左右子
2015-05-11 12:06:15 353
原创 Leetcode Same Tree 判断两棵二叉树是否相同
题目:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
2015-05-11 11:53:57 567
原创 Leetcode Sort List 链表排序
题目:Sort a linked list in O(n log n) time using constant space complexity.分析:选择用归并排序来解决。但是找中点的时候需要注意对于 1 --> 2 --> null 的情况,我们需要mid为1,才能把1和2分开,找中点的时候让fast先走一步即可。1. 如果链表为空或只有一个节点
2015-05-11 11:19:03 245
原创 Leetcode Reverse Nodes in k-Group
题目:Given a linked list, reverse the nodes of a linked list k at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should re
2015-05-10 16:00:47 216
原创 Leetcode Reorder List 链表重排序
题目:Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, r
2015-05-10 11:42:39 320
原创 Leetcode Convert Sorted List to Binary Search Tree 把有序链表转换成二叉搜索树
题目:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.分析:举一个例子, 假如链表为 1--> 2 --> 3 --> 4 --> 5 --> 6 --> 7。 根据二叉搜索树的性质 root.va
2015-05-10 10:13:49 453
原创 Leetcode Rotate List 旋转链表
题目:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.分析:1. k的值可能大于链
2015-05-09 14:31:15 374
原创 Leetcode Merge k Sorted Lists 合并k个链表
题目:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.分析:1. 用暴力的方法一个一个合并链表会超时,分析所需要的时间复杂度:假设链表的长度为n,每两个链表合并所需要的时间为n+n,k个链表一共需要合并k
2015-05-09 14:03:05 373
原创 Leetcode Swap Nodes in Pairs 交换链表的节点对
题目:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only cons
2015-05-08 20:43:48 292
原创 Leetcode Remove Nth Node From End of List 删除链表倒数第n个元素
题目:Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from
2015-05-08 18:41:56 283
原创 Leetcode Copy List with Random Pointer 拷贝链表
题目:A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.分析:1. 遍历链表,
2015-05-08 17:02:46 279
原创 Leetcode Linked List Cycle II 循环链表入口
题目:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.分析:个人觉得这题的原理没有必要一定用数学公式深究,记住这个方法就可以,因为对别的题并不具备推广性,有兴趣的话网上的数学推导也有很多。1. 首先利用快慢
2015-05-08 15:39:32 321
原创 Leetcode Partition List 分割链表
题目:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nod
2015-05-07 21:38:59 464
原创 Leetcode Add Two Numbers 两个链表表示的数相加
题目:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return
2015-05-07 20:00:07 469
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人