- 博客(126)
- 收藏
- 关注
原创 1. Two Sum
题目描述题目链接https://leetcode.com/problems/two-sum/方法思路Approach1:public int[] twoSum(int[] nums, int target) { for(int i = 0; i < nums.length; i++){ for(int j = nums.length - 1...
2019-04-04 11:06:57
228
原创 987. Vertical Order Traversal of a Binary Tree
题目描述Given a binary tree, return the vertical order traversal of its nodes values.For each node at position (X, Y), its left and right children respectively will be at positions (X-1, Y-1) and (X+1, ...
2019-04-03 11:28:01
456
原创 863. All Nodes Distance K in Binary Tree
题目描述We are given a binary tree (with root node root), a target node, and an integer value K.Return a list of the values of all nodes that have a distance K from the target node. The answer can be r...
2019-04-03 09:57:37
262
原创 958. Check Completeness of a Binary Tree
题目描述Given a binary tree, determine if it is a complete binary tree.Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely...
2019-04-02 22:15:45
230
原创 662. Maximum Width of Binary Tree
题目描述Given 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 full binar...
2019-04-02 21:24:56
185
转载 968. Binary Tree Cameras
题目描述Given a binary tree, we install cameras on the nodes of the tree.Each camera at a node can monitor its parent, itself, and its immediate children.Calculate the minimum number of cameras needed ...
2019-04-01 22:30:33
226
原创 297. Serialize and Deserialize Binary Tree
题目描述题目链接方法思路Approach1:The idea is simple: print the tree in pre-order traversal and use “X” to denote null node and split node with “,”. We can use a StringBuilder for building the string on the f...
2019-04-01 20:36:59
179
原创 99. Recover Binary Search Tree
题目描述Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.题目链接https://leetcode.com/problems/recover-binary-search-tree/方法思路public cla...
2019-04-01 17:23:11
210
原创 501. Find Mode in Binary Search Tree
题目描述题目链接https://leetcode.com/problems/find-mode-in-binary-search-tree/方法思路Approach1: based on HashMapclass Solution { //Runtime: 6 ms, faster than 51.99% //Memory Usage: 40.3 MB, less th...
2019-03-31 20:34:14
188
原创 449. Serialize and Deserialize BST
题目描述Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link ...
2019-03-31 14:22:46
151
原创 337. House Robber III
题目描述The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the “root.” Besides the root, each house has one and only one parent house. After a...
2019-03-30 20:12:47
160
原创 213. House Robber II
题目描述题目链接https://leetcode.com/problems/house-robber-ii/方法思路class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 35.5 MB, less than 94.44% public int rob(int[] nums) {...
2019-03-29 15:59:01
140
翻译 198. House Robber 关于动态规划问题(Dynamic programming)的学习、思考与记录
动态规划(Dynamic programming)定义任何数学递推公式都可以直接转换为递归算法,但是基本现实是编译器常常不能正确对待递归算法,结果导致低效的程序。当怀疑出现这样的情况时,我们必须再给编译器提供一些帮助,即将递归算法转换为非递归算法,让后者把那些子问题的答案系统的记录在一个表内。利用这种方法的一种技巧叫做动态规划。以LeetCode.198. House Robber为例进行一...
2019-03-28 22:37:15
200
原创 652. Find Duplicate Subtrees(寻找重复的子树)
题目描述Given 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 the same st...
2019-03-28 20:14:06
231
原创 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 node...
2019-03-27 21:19:58
141
原创 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 betw...
2019-03-27 20:51:24
221
原创 222. Count Complete Tree Nodes(求完美二叉树的节点数)
题目描述题目链接https://leetcode.com/problems/count-complete-tree-nodes/方法思路Approach1:recursive dfs 很常规的方法class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 41.1 MB, less than...
2019-03-27 15:25:07
235
原创 199. Binary Tree Right Side View(二叉树的右视图)
题目描述题目链接https://leetcode.com/problems/binary-tree-right-side-view/方法思路Apprach1:基于层序遍历,只添加每层的最后一个节点的值。class Solution { //Runtime: 1 ms, faster than 72.32% //Memory Usage: 37.1 MB, less t...
2019-03-27 14:52:15
339
原创 284. Peeking Iterator
题目描述题目链接https://leetcode.com/problems/peeking-iterator/方法思路Approach1:class PeekingIterator implements Iterator<Integer> { //Runtime: 47 ms, faster than 99.35% //Memory Usage: 37 MB...
2019-03-26 20:47:05
250
原创 173. Binary Search Tree Iterator
题目描述题目链接https://leetcode.com/problems/binary-search-tree-iterator/方法思路Approach:using Queueclass BSTIterator { //Runtime: 59 ms, faster than 91.88% //Memory Usage: 52.7 MB, less than 9....
2019-03-26 20:25:23
130
原创 117. Populating Next Right Pointers in Each Node II
题目描述题目链接https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/方法思路Approach1:class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 52.9 MB, less t...
2019-03-26 19:39:52
135
原创 543. Diameter of Binary Tree(二叉树的直径)
题目描述题目链接https://leetcode.com/problems/diameter-of-binary-tree/方法思路Approach1: 双递归但是效率很低,这个方法太过直接粗暴class Solution { //Runtime: 13 ms, faster than 14.48% //Memory Usage: 40.6 MB, less than ...
2019-03-25 22:45:54
167
原创 116. Populating Next Right Pointers in Each Node
题目描述Output: {“KaTeX parse error: Expected '}', got 'EOF' at end of input: …":"1","left":{"id”:“2”,“left”:{“KaTeX parse error: Expected '}', got 'EOF' at end of input: …:null,"next":{"id”:“4”,“left”:...
2019-03-25 21:55:57
134
原创 103. Binary Tree Zigzag Level Order Traversal
题目描述题目链接https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/方法思路类似于层序遍历,使用了队列的数据结构,但是不清楚为什么LinkedList的方法addFirst() 用不了。只能用ArrayList的 add(int index, E e) 方法来实现遍历。class Solution...
2019-03-25 20:56:11
139
原创 988. Smallest String Starting From Leaf
题目描述Given the root of a binary tree, each node has a value from 0 to 25 representing the letters ‘a’ to ‘z’: a value of 0 represents ‘a’, a value of 1 represents ‘b’, and so on.Find the lexicographi...
2019-03-24 13:07:34
339
原创 572. Subtree of Another Tree
题目描述Given 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 of this...
2019-03-24 10:54:36
164
原创 508. Most Frequent Subtree Sum
题目描述题目链接https://leetcode.com/problems/most-frequent-subtree-sum/方法思路class Solution { //Runtime: 7 ms, faster than 86.00% //Memory Usage: 40.6 MB, less than 39.71% //键值对的映射,取sum为key,fr...
2019-03-24 09:29:07
123
原创 865. Smallest Subtree with all the Deepest Nodes
题目描述方法思路错误的思路:只考虑了示例中的情况,没想周全class Solution { Map<TreeNode, TreeNode> parent_children;// = new HashMap<>() Map<TreeNode, Integer> node_depth;// = new HashMap<>() ...
2019-03-23 15:45:41
179
原创 655. Print Binary Tree(打印二叉树)
题目描述方法思路public class Solution { //Memory Usage: 37.8 MB, less than 5.71% //Runtime: 3 ms, faster than 100.00% public List<List<String>> printTree(TreeNode root) { in...
2019-03-23 13:40:44
281
原创 515. Find Largest Value in Each Tree Row
题目描述方法思路很简单的题目,我用了层序遍历的方法class Solution { //Runtime: 4 ms, faster than 92.82% //Memory Usage: 40.1 MB, less than 46.20% public List<Integer> largestValues(TreeNode root) { ...
2019-03-23 11:25:27
146
原创 513. Find Bottom Left Tree Value
题目描述Given a binary tree, find the leftmost value in the last row of the tree.方法思路Appraoch1:简单粗暴的方法class Solution { //Runtime: 3 ms, faster than 95.43% //Memory Usage: 39.8 MB, less tha...
2019-03-22 14:30:42
201
原创 889. Construct Binary Tree from Preorder and Postorder Traversal
题目描述方法思路class Solution { //Runtime: 9 ms, faster than 75.00% //Memory Usage: 39.7 MB, less than 91.45% public TreeNode constructFromPrePost(int[] pre, int[] post) { int N = pre....
2019-03-22 13:49:29
124
原创 979. Distribute Coins in Binary Tree
题目描述Given the root of a binary tree with N nodes, each node in the tree has node.val coins, and there are N coins total.In one move, we may choose two adjacent nodes and move one coin from one node ...
2019-03-22 11:38:52
157
原创 404. Sum of Left Leaves
题目描述方法思路很简单的题目可以递归,也可以迭代Approach1:iterativeclass Solution { public int sumOfLeftLeaves(TreeNode root) { if(root == null) return 0; int ans = 0; Stack<TreeNode> stack = new S...
2019-03-21 20:40:12
194
原创 95. Unique Binary Search Trees II
题目描述方法思路class Solution{ //Runtime: 2 ms, faster than 68.65% //Memory Usage: 37.1 MB, less than 99.04% public List<TreeNode> generateTrees(int n) { if(n == 0) return new A...
2019-03-21 15:53:14
146
原创 96. Unique Binary Search Trees
题目描述方法思路这是一个动态规划问题;class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 31.7 MB, less than 100.00% public int numTrees(int n) { if(n == 0 || n == 1) return 1;...
2019-03-21 12:17:48
136
原创 684. Redundant Connection(冗余连接)
题目描述方法思路class Solution { //Runtime: 2 ms, faster than 100.00% //Memory Usage: 41.2 MB, less than 11.55% public int[] findRedundantConnection(int[][] edges) { int[] parent = new...
2019-03-20 12:34:41
305
原创 951. Flip Equivalent Binary Trees(反转等效二叉树)
题目描述For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.A binary tree X is flip equivalent to a binary tree Y if and only if w...
2019-03-20 11:51:13
248
原创 623. Add One Row to Tree(在二叉树中增加一行)
题目描述方法思路class Solution { //Runtime: 4 ms, faster than 93.26% //Memory Usage: 40.1 MB, less than 59.02% public TreeNode addOneRow(TreeNode root, int v, int d) { if(d == 1){ ...
2019-03-20 10:04:39
241
原创 894. All Possible Full Binary Trees(所有可能的完美二叉树)
题目描述方法思路class Solution { //Runtime: 3 ms, faster than 95.78% //Memory Usage: 44 MB, less than 68.95% Map<Integer, List<TreeNode>> memo = new HashMap(); public List<T...
2019-03-19 22:46:01
200
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人