LeetCode
Ssunsets
这个作者很懒,什么都没留下…
展开
-
LeetCode(101) 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 3But the following is not:原创 2015-12-09 17:39:01 · 417 阅读 · 0 评论 -
LeetCode(144) 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 / 3解题思路:递归解法是很普通的先序遍历,非递归的解法灵感来源于我的解题报告LeetCode(114),看不懂的话可以去看原创 2015-12-15 17:09:51 · 449 阅读 · 0 评论 -
LeetCode(222) Count Complete Tree Nodes解题报告
Given a complete binary tree, count the number of nodes.解题思路: 直接暴力求解超时,注意到一个完全二叉树,它的任何子树,要么是一个满二叉树,要么是一个完全二叉树。通过求一个二叉树最左的深度,最右的深度,来判断是不是满二叉树,如果不是满二叉树,则递归求解,如果是则套用公式(2 << 深度) - 1。public class Solution原创 2015-12-16 09:21:17 · 755 阅读 · 0 评论 -
LeetCode(236) Lowest Common Ancestor of a Binary Tree解题报告
Given 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 two nodes v and原创 2015-12-17 10:56:05 · 688 阅读 · 0 评论 -
LeetCode(26) Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2016-01-19 08:47:22 · 338 阅读 · 0 评论 -
LeetCode(80) Remove Duplicates from Sorted Array II
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first five elemen原创 2016-01-19 11:08:23 · 354 阅读 · 0 评论 -
LeetCode(33) Search in Rotated Sorted Array解题报告
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index原创 2016-01-19 13:17:26 · 380 阅读 · 0 评论 -
LeetCode(303) Range Sum Query - Immutable解题报告
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example: Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRang原创 2015-12-04 13:11:35 · 822 阅读 · 0 评论 -
LeetCode(306) Addtive Number解题报告
Additive number is a string whose digits can form additive sequence. A valid additive sequence should contain at least three numbers. Except for the first two numbers, each subsequent number in the se原创 2015-12-04 11:59:20 · 1074 阅读 · 0 评论 -
LeetCode(292) Nim Game解题报告
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2015-12-05 19:51:18 · 655 阅读 · 0 评论 -
LeetCode(94) 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: Recursive solution is trivial, could yo原创 2015-12-08 10:20:50 · 368 阅读 · 0 评论 -
LeetCode(95) Unique Binary Search Trees II解题报告
Given n, generate all structurally unique BST’s (binary search trees) that store values 1…n.For example, Given n = 3, your program should return all 5 unique BST’s shown below. 1 3 3原创 2015-12-07 21:42:21 · 632 阅读 · 0 评论 -
LeetCode(96) Unique Binary Search Trees解题报告
Given n, how many structurally unique BST’s (binary search trees) that store values 1…n?For example, Given n = 3, there are a total of 5 unique BST’s. 1 3 3 2 1 \ /原创 2015-12-07 20:13:34 · 547 阅读 · 0 评论 -
LeetCode(98) Validate Binary Search Tree解题报告
Given 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 node’s key. The right原创 2015-12-08 12:16:22 · 601 阅读 · 0 评论 -
LeetCode(99) Recover Binary Search Tree解题报告
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is pretty straight forward. Could you devise a const原创 2015-12-09 19:48:06 · 422 阅读 · 0 评论 -
LeetCode(173) 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.Note: next() and hasN原创 2015-12-15 22:43:38 · 408 阅读 · 0 评论 -
LeetCode(235) Lowest Common Ancestor of a Binary Search Tree解题报告
Given 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 lowest common ancestor is defined between two原创 2015-12-16 10:24:34 · 378 阅读 · 0 评论 -
LeetCode(102) 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 / \ 9 20 / \ 1原创 2015-12-10 08:22:38 · 366 阅读 · 0 评论 -
LeetCode(103) 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: Given binary tree原创 2015-12-10 09:08:37 · 363 阅读 · 0 评论 -
LeetCode(97) Interleaving String解题报告
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example, Given: s1 = “aabcc”, s2 = “dbbca”,When s3 = “aadbbcbcac”, return true. When s3 = “aadbbbaccc”, return false原创 2015-12-10 12:02:36 · 882 阅读 · 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.解题思路: 先序遍历的第一个结点一定是根节点,查找根节点在中序遍历数组中的位置,前面的结点便是左子树,后面的便是右子树,然后递原创 2015-12-12 22:39:59 · 512 阅读 · 0 评论 -
LeetCode(106) Construct Binary Tree from Inorder and Postorder Traversal解题报告
Given inorder and postorder traversal of a tree, construct the binary tree.解题思路:后序加中序构造树,会先序加中序就会后序加中序,不会的话看上一个题。。。。。。/** * Definition for a binary tree node. * public class TreeNode { * int val原创 2015-12-12 23:21:11 · 390 阅读 · 0 评论 -
LeetCode(107) 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,20,#,#,15,7}, 3原创 2015-12-13 08:52:18 · 313 阅读 · 0 评论 -
LeetCode(108) Convert Sorted Array to Binary Search Tree解题报告
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路: 取数组中点mid作为根节点,从start到mid-1作为左子树,mid+1到end作为右子树,递归调用即可。/** * Definition for a binary tree node.原创 2015-12-13 09:17:56 · 309 阅读 · 0 评论 -
LeetCode(109) 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.解题思路: 将节点转化为数组,然后递归求解public class Solution { public TreeNode sortedListToBST(ListNode h原创 2015-12-13 09:44:10 · 285 阅读 · 0 评论 -
LeetCode(110) 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 node never differ by原创 2015-12-13 12:56:31 · 391 阅读 · 0 评论 -
LeetCode(114) Flatten Binary Tree to Linked List解题报告
Given 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-12-14 09:57:42 · 505 阅读 · 0 评论 -
LeetCode(111) 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.解题思路:递归求解,如果有比min还小的深度,就更新min,需要注意的是,最小深度是叶原创 2015-12-13 13:30:06 · 456 阅读 · 0 评论 -
LeetCode(124) Binary Tree Maximum Path Sum解题报告
Given 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-child connections. The path原创 2015-12-14 11:50:46 · 446 阅读 · 0 评论 -
LeetCode(145) 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].解题报告:递归解法是普通的后续遍历,不用多说,非递归用了一个栈来存储结点,想了好久也没原创 2015-12-15 19:02:50 · 788 阅读 · 0 评论 -
LeetCode(199) 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 tree, 1原创 2015-12-16 08:13:45 · 430 阅读 · 0 评论 -
LeetCode(254) Binary Tree Paths解题报告
Given 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"]解题思路: 代码应该看得懂,先序遍历,处理字符串。public原创 2015-12-16 11:01:12 · 455 阅读 · 0 评论