leetcode
交流,讨论,分享leetcode中题目的各种解法
kevin聪
Success is not final.Failure is not fatal.It is the courage to continue that counts.
展开
-
145-Binary Tree Postorder Traversal
DescriptionGiven a binary tree, return the postorder traversal of its nodes’ values.Example:Input: [1,null,2,3] 1 \ 2 / 3Output: [3,2,1]Follow up: Recursive solution is ...原创 2018-05-28 15:57:06 · 265 阅读 · 0 评论 -
733-Flood Fill
DescriptionAn image is represented by a 2-D array of integers, each integer representing the pixel value of the image (from 0 to 65535).Given a coordinate (sr, sc) representing the starting pixel ...原创 2018-05-28 16:09:44 · 389 阅读 · 0 评论 -
529-Minesweeper
DescriptionLet’s play the minesweeper game (Wikipedia, online game)!You are given a 2D char matrix representing the game board. ‘M’ represents an unrevealed mine, ‘E’ represents an unrevealed empt...原创 2018-05-28 17:01:56 · 405 阅读 · 0 评论 -
547-Friend Circles
DescriptionThere are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct frien...原创 2018-05-28 17:47:06 · 285 阅读 · 0 评论 -
491-Increasing Subsequences
DescriptionGiven an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 .E...原创 2018-05-28 21:55:25 · 301 阅读 · 0 评论 -
785-Is Graph Bipartite?
DescriptionGiven an undirected graph, return true if and only if it is bipartite.Recall that a graph is bipartite if we can split it’s set of nodes into two independent subsets A and B such that e...原创 2018-05-28 22:19:47 · 478 阅读 · 0 评论 -
802-Find Eventual Safe States
DescriptionIn a directed graph, we start at some node and every turn, walk along a directed edge of the graph. If we reach a node that is terminal (that is, it has no outgoing directed edges), we s...原创 2018-05-28 22:49:27 · 392 阅读 · 0 评论 -
200-Number of Islands
DescriptionGiven 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. Y...原创 2018-05-28 22:55:00 · 293 阅读 · 0 评论 -
473-Matchsticks to Square
DescriptionRemember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchstic...原创 2018-05-28 23:14:04 · 351 阅读 · 0 评论 -
662-Maximum Width of Binary Tree
DescriptionGiven a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a fu...原创 2018-05-25 09:47:43 · 299 阅读 · 0 评论 -
129-Sum Root to Leaf Numbers
DescriptionGiven 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....原创 2018-05-25 10:06:52 · 211 阅读 · 0 评论 -
450-Delete Node in a BST
DescriptionGiven a root node reference of a BST and a key, delete the node with the given key in the BST. Return the root node reference (possibly updated) of the BST.Basically, the deletion can b...原创 2018-05-25 10:28:03 · 184 阅读 · 0 评论 -
652-Find Duplicate Subtrees
DescriptionGiven a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.Two trees are duplicate if they have th...原创 2018-05-25 10:45:06 · 207 阅读 · 0 评论 -
114-Flatten Binary Tree to Linked List
DescriptionGiven a binary tree, flatten it to a linked list in-place.For example, given the following tree: 1 / \ 2 5 / \ \3 4 6The flattened tree should look like:1 \...原创 2018-05-25 11:10:14 · 206 阅读 · 0 评论 -
116-Populating Next Right Pointers in Each Node
DescriptionGiven a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is ...原创 2018-05-25 11:25:54 · 169 阅读 · 0 评论 -
113-Path Sum II
DescriptionGiven a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum.Note: A leaf is a node with no children.Example:Given the below binary tree a...原创 2018-05-25 13:51:08 · 186 阅读 · 0 评论 -
117-Populating Next Right Pointers in Each Node II
DescriptionGiven a binary treestruct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next;}Populate each next pointer to point to its next right node. If there is ...原创 2018-05-25 14:18:27 · 272 阅读 · 0 评论 -
236-Lowest Common Ancestor of a Binary Tree
DescriptionGiven 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 ancestor is defined between...原创 2018-05-25 14:45:47 · 315 阅读 · 0 评论 -
98-Validate Binary Search Tree
DescriptionGiven a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the nod...原创 2018-05-25 14:59:26 · 306 阅读 · 0 评论 -
318-Maximum Product of Word Lengths
DescriptionGiven a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lo...原创 2018-04-25 17:55:30 · 151 阅读 · 0 评论 -
137-Single Number II
DescriptionGiven a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note:Your algorithm should have a linear runtim...原创 2018-04-25 23:04:59 · 162 阅读 · 0 评论 -
393-UTF-8 Validation
DescriptionA character in UTF8 can be from 1 to 4 bytes long, subjected to the following rules:For 1-byte character, the first bit is a 0, followed by its unicode code.For n-bytes character, the...原创 2018-04-25 23:07:59 · 225 阅读 · 0 评论 -
201-Bitwise AND of Numbers Range
DescriptionGiven a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the range [5, 7], you should return 4....原创 2018-04-25 23:11:54 · 217 阅读 · 0 评论 -
617-Merge Two Binary Trees
DescriptionGiven two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.You need to merge them into a ne...原创 2018-04-25 23:22:36 · 152 阅读 · 0 评论 -
669-Trim a Binary Search Tree
DescriptionGiven a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tr...原创 2018-04-25 23:30:29 · 188 阅读 · 0 评论 -
637-Average of Levels in Binary Tree
DescriptionGiven a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5,...原创 2018-04-25 23:35:58 · 153 阅读 · 0 评论 -
104-Maximum Depth of Binary Tree
DescriptionGiven 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.Note: A leaf is a node wi...原创 2018-04-25 23:38:05 · 170 阅读 · 0 评论 -
226-Invert Binary Tree
DescriptionInvert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia: This problem was inspired by this original tweet ...原创 2018-04-25 23:41:57 · 172 阅读 · 0 评论 -
653-Two Sum IV - Input is a BST
DescriptionGiven a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the given target.Example 1:Input: 5 / \...原创 2018-04-25 23:46:18 · 137 阅读 · 0 评论 -
538-Convert BST to Greater Tree
DescriptionGiven a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in...原创 2018-04-26 00:15:43 · 167 阅读 · 0 评论 -
100-Same Tree
DescriptionGiven two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same val...原创 2018-04-30 10:28:07 · 142 阅读 · 0 评论 -
404-Sum of Left Leaves
DescriptionFind the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectiv...原创 2018-04-30 10:32:29 · 153 阅读 · 0 评论 -
563-Binary Tree Tilt
DescriptionGiven a binary tree, return the tilt of the whole tree.The tilt of a tree node is defined as the absolute difference between the sum of all left subtree node values and the sum of all r...原创 2018-04-30 10:37:40 · 260 阅读 · 0 评论 -
543-Diameter of Binary Tree
DescriptionGiven a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path...原创 2018-04-30 10:47:19 · 334 阅读 · 0 评论 -
108-Convert Sorted Array to Binary Search Tree
DescriptionGiven an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the...原创 2018-05-02 19:45:20 · 119 阅读 · 0 评论 -
107-Binary Tree Level Order Traversal II
DescriptionGiven 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,20,...原创 2018-05-02 19:51:31 · 124 阅读 · 0 评论 -
671-Second Minimum Node In a Binary Tree
DescriptionGiven a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the node has two sub-nodes, then t...原创 2018-05-02 20:09:20 · 126 阅读 · 0 评论 -
257-Binary Tree Paths
DescriptionGiven a binary tree, return all root-to-leaf paths.For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf pAll root-to-leaf paths are:["1-&...原创 2018-05-02 20:17:08 · 128 阅读 · 0 评论 -
101-Symmetric Tree
DescriptionGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ ...原创 2018-05-02 20:29:45 · 134 阅读 · 0 评论 -
572-Subtree of Another Tree
DescriptionGiven two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all...原创 2018-05-02 20:35:25 · 167 阅读 · 0 评论