
leetcode
文章平均质量分 50
yongyuandeie
这个作者很懒,什么都没留下…
展开
-
LeetCode 103: Binary Tree Zigzag Level Order Traversal
Binary Tree Zigzag Level Order TraversalGiven 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 b原创 2015-10-27 23:10:32 · 232 阅读 · 0 评论 -
LeetCode 129: Sum Root to Leaf Numbers
Sum Root to Leaf NumbersGiven 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-10-28 21:24:30 · 271 阅读 · 0 评论 -
LeetCode 257: Binary Tree Paths
Binary Tree PathsGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]解题思路代码如下:/**原创 2015-10-29 10:15:04 · 308 阅读 · 0 评论 -
LeetCode 235: Lowest Common Ancestor of a Binary Search Tree
Lowest Common Ancestor of a Binary Search TreeGiven a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The转载 2015-10-29 10:40:15 · 237 阅读 · 0 评论 -
LeetCode 236: Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary TreeGiven a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ance原创 2015-10-29 13:51:47 · 346 阅读 · 0 评论 -
LeetCode 114: Flatten Binary Tree to Linked List
Flatten Binary Tree to Linked ListGiven a binary tree, flatten it to a linked list in-place.For example, Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:1 \ 2转载 2015-10-28 16:20:53 · 251 阅读 · 0 评论 -
LeetCode 117: Populating Next Right Pointers in Each Node II
Populating Next Right Pointers in Each Node IIFollow 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 wor原创 2015-10-28 19:52:32 · 387 阅读 · 0 评论 -
LeetCode 198: House Robber
House RobberYou 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 adj原创 2015-10-29 19:01:01 · 275 阅读 · 0 评论 -
LeetCode 213: House Robber II
House Robber IINote: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention.转载 2015-10-29 19:29:30 · 240 阅读 · 0 评论 -
LeetCode 121: Best Time to Buy and Sell Stock
Best Time to Buy and Sell StockSay 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 sel转载 2015-10-29 20:27:24 · 272 阅读 · 0 评论 -
LeetCode 145: Binary Tree Postorder Traversal
Binary Tree Postorder TraversalGiven 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: Recursive solution i转载 2015-10-28 22:20:12 · 257 阅读 · 0 评论 -
LeetCode 144: Binary Tree Preorder Traversal
Binary Tree Preorder TraversalGiven 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: Recursive solution is原创 2015-10-28 21:56:29 · 256 阅读 · 0 评论 -
LeetCode 94: Binary Tree Inorder Traversal
Binary Tree Inorder TraversalGiven a binary tree, return the inorder traversal of its nodes’ values.For example: Given binary tree {1,#,2,3}, return [1,3,2].Note: Recursive solution is trivial, cou原创 2015-10-27 14:45:04 · 337 阅读 · 0 评论 -
LeetCode 40: Combination Sum II
Combination Sum IIGiven a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used once in原创 2015-10-25 16:27:48 · 360 阅读 · 0 评论 -
LeetCode 101: Symmetric Tree
Symmetric TreeGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: But the following is not: Note: Bonus points转载 2015-10-27 20:34:12 · 264 阅读 · 0 评论 -
LeetCode 106: Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Inorder and Postorder TraversalGiven inorder and postorder traversal of a tree, construct the binary tree.Note: You may assume that duplicates do not exist in the tree.解题思路方原创 2015-10-28 10:13:36 · 253 阅读 · 0 评论 -
LeetCode 124: Binary Tree Maximum Path Sum
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum.For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-转载 2015-10-28 21:00:03 · 278 阅读 · 0 评论 -
LeetCode 107: Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal IIGiven 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原创 2015-10-28 11:04:06 · 245 阅读 · 0 评论 -
LeetCode 108: Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search TreeGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路 为了得到高度平衡的二叉树,选中点 middle=(start+end)/2middle = (start + e原创 2015-10-28 12:57:43 · 255 阅读 · 0 评论 -
LeetCode 110: Balanced Binary Tree
Balanced Binary TreeGiven 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 n原创 2015-10-28 14:01:00 · 304 阅读 · 0 评论 -
LeetCode 111: Minimum Depth of Binary Tree
Minimum Depth of Binary TreeGiven 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-10-28 14:36:00 · 246 阅读 · 0 评论 -
LeetCode 122: Best Time to Buy and Sell Stock II
Best Time to Buy and Sell Stock IISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many transac原创 2015-10-29 20:45:53 · 458 阅读 · 0 评论 -
LeetCode 116: Populating Next Right Pointers in Each Node
Given a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is no next right node,原创 2015-10-28 19:09:30 · 487 阅读 · 0 评论 -
LeetCode 230: Kth Smallest Element in a BST
Kth Smallest Element in a BSTGiven a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements.Follo转载 2015-10-29 10:02:48 · 332 阅读 · 0 评论 -
LeetCode 70: Climbing Stairs
Climbing StairsYou 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?解题思路class Solution {p原创 2015-10-30 10:25:10 · 248 阅读 · 0 评论 -
LeetCode 62: 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 to reach the botto原创 2015-10-30 10:41:31 · 271 阅读 · 0 评论 -
LeetCode 53: Maximum Subarray
Maximum SubarrayFind the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4], the contiguous subarray [4原创 2015-10-30 13:49:35 · 271 阅读 · 0 评论 -
LeetCode 137: Single Number II
Single Number IIGiven an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it转载 2015-10-30 15:17:04 · 239 阅读 · 0 评论 -
LeetCode 190: Reverse Bits
Reverse BitsReverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00原创 2015-10-30 16:19:54 · 253 阅读 · 0 评论 -
LeetCode 191: Number of 1 Bits
Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representati原创 2015-10-30 16:22:30 · 363 阅读 · 0 评论 -
LeetCode 78: Subsets
Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example, If num原创 2015-10-30 14:57:55 · 276 阅读 · 0 评论 -
LeetCode 136: Single Number
Single NumberGiven 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 u原创 2015-10-30 15:02:09 · 305 阅读 · 0 评论 -
LeetCode 169: Majority Element
Majority ElementGiven an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority原创 2015-10-30 15:30:43 · 254 阅读 · 0 评论 -
LeetCode 91: Decode Ways
Decode WaysA message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total转载 2015-10-30 10:20:34 · 245 阅读 · 0 评论 -
LeetCode 63: Unique Paths II
Unique Paths IIFollow 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 respectively in原创 2015-10-30 13:37:44 · 242 阅读 · 0 评论 -
LeetCode 279: Perfect Squares
Perfect SquaresGiven a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.For example, given n = 12, return 3 because 12 = 4 + 4 + 4; giv原创 2015-10-29 14:42:33 · 282 阅读 · 0 评论 -
LeetCode 264: Ugly Number II
Ugly Number IIWrite a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the转载 2015-10-29 15:18:20 · 287 阅读 · 0 评论 -
LeetCode 112: Path Sum
Path SumGiven 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 tree and原创 2015-10-28 14:45:41 · 282 阅读 · 0 评论 -
LeetCode 113: Path Sum II
Path Sum IIGiven 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,return 解题思路深度优先搜索。代码如下:/** * De转载 2015-10-28 15:18:58 · 365 阅读 · 0 评论 -
LeetCode 221: Maximal Square
Maximal SquareGiven a 2D binary matrix filled with 0’s and 1’s, find the largest square containing all 1’s and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0原创 2015-10-29 15:46:12 · 279 阅读 · 0 评论