温故知新
N.C_IPOC_BUPT
一个喜欢科怀.莱昂纳德的准程序猿,研一在读。
展开
-
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 · 414 阅读 · 0 评论 -
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 · 128 阅读 · 0 评论 -
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 · 153 阅读 · 0 评论 -
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 · 256 阅读 · 0 评论 -
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 · 148 阅读 · 0 评论 -
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 · 112 阅读 · 0 评论 -
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 · 108 阅读 · 0 评论 -
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 · 138 阅读 · 0 评论 -
606. Construct String from Binary Tree(根据二叉树构建字符串)
题目描述You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair “()”. And ...原创 2019-03-18 17:09:08 · 133 阅读 · 0 评论 -
671. Second Minimum Node In a Binary Tree(二叉树中第二小的节点)
题目描述给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0。如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值。给出这样的一个二叉树,你需要输出所有节点中的第二小的值。如果第二小的值不存在的话,输出 -1 。方法思路Approach #1: Brute Force [Accepted]raverse the tree with a d...原创 2019-03-13 11:57:03 · 172 阅读 · 0 评论 -
98. Validate Binary Search Tree(验证是否为二叉树搜索树)
题目描述方法思路Approach1: recursive + inorder traversalCompute inorder traversal list inorder.Check if each element in inorder is smaller than the next one.class Solution{ //Runtime: 1 ms, faster th...原创 2019-03-13 10:10:43 · 155 阅读 · 0 评论 -
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 · 117 阅读 · 0 评论 -
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 · 225 阅读 · 0 评论 -
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 · 141 阅读 · 0 评论 -
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 · 225 阅读 · 0 评论 -
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 · 160 阅读 · 0 评论 -
198. House Robber 关于动态规划问题(Dynamic programming)的学习、思考与记录
动态规划(Dynamic programming)定义任何数学递推公式都可以直接转换为递归算法,但是基本现实是编译器常常不能正确对待递归算法,结果导致低效的程序。当怀疑出现这样的情况时,我们必须再给编译器提供一些帮助,即将递归算法转换为非递归算法,让后者把那些子问题的答案系统的记录在一个表内。利用这种方法的一种技巧叫做动态规划。以LeetCode.198. House Robber为例进行一...翻译 2019-03-28 22:37:15 · 171 阅读 · 0 评论 -
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 · 202 阅读 · 0 评论 -
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 · 194 阅读 · 0 评论 -
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 · 151 阅读 · 0 评论 -
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 · 179 阅读 · 0 评论 -
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 · 207 阅读 · 0 评论 -
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 · 167 阅读 · 0 评论 -
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 · 124 阅读 · 0 评论 -
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 · 314 阅读 · 0 评论 -
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 · 122 阅读 · 0 评论 -
101. Symmetric Tree
题目描述方法思路Approach1: iterativeclass Solution{ //Runtime: 5 ms, faster than 100.00% //Memory Usage: 39 MB, less than 30.40% public boolean isSymmetric(TreeNode root) { Queue<...原创 2019-03-17 20:51:02 · 106 阅读 · 0 评论 -
129. 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.Find the t...原创 2019-03-15 10:03:00 · 139 阅读 · 0 评论 -
563. Binary Tree Tilt(二叉树的坡度)
题目描述方法思路public class Solution { //真是个莫名其妙的的题目 //Runtime: 3 ms, faster than 100.00% //Memory Usage: 40.8 MB, less than 10.53% int tilt=0; public int findTilt(TreeNode root) { ...原创 2019-03-15 09:02:58 · 136 阅读 · 0 评论 -
450. Delete Node in a BST(在二叉树搜索树中删除节点)
题目描述方法思路class Solution { //Runtime: 3 ms, faster than 98.89% //Memory Usage: 41.4 MB, less than 35.22% public TreeNode deleteNode(TreeNode root, int key) { if(root == null) retu...原创 2019-03-14 11:28:13 · 295 阅读 · 0 评论 -
998. Maximum Binary Tree II(最大二叉树II)
题目描述方法思路Q1:Why to the right and not to the left?Always go right since new element will be inserted at the end of the list.Q2:why if(root.val&lt;v){TreeNode node = new TreeNode(v);node.left=root;...原创 2019-03-14 11:20:35 · 209 阅读 · 0 评论 -
147. Insertion Sort List(对链表进行插入排序)
题目描述方法思路创建一个辅助的新链表,并且使用一个指针遍历原链表,每次将原链表中的一个节点插入到新链表的合适位置(即该节点的值大于新链表上的节点的值,又小于后一节点的值)。最后将新链表的头部返回即可。class Solution { //Runtime: 32 ms, faster than 34.04% //Memory Usage: 37.9 MB, less than...原创 2019-03-01 12:52:22 · 93 阅读 · 0 评论 -
61. Rotate List(旋转链表)
题目描述Given a linked list, rotate the list to the right by k places, where k is non-negative.Example 1:Input: 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5-&amp;gt;NULL, k = 2Output: 4-&amp;gt;5-&am原创 2019-02-28 16:10:00 · 171 阅读 · 0 评论 -
92. Reverse Linked List II
题目描述Reverse a linked list from position m to n. Do it in one-pass.Note: 1 ≤ m ≤ n ≤ length of list.Example:Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;NULL, m = 2, n = 4Output: 1-&gt;4-&gt;3-&a原创 2019-02-28 16:05:18 · 85 阅读 · 0 评论 -
148. Sort List
题目描述Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4-&gt;2-&gt;1-&gt;3Output: 1-&gt;2-&gt;3-&gt;4Example 2:Input: -1-&gt;5-&gt;3-&gt原创 2019-03-01 12:44:18 · 115 阅读 · 0 评论 -
707. Design Linked List(设计链表)
题目描述设计链表的实现。您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。在链表类中实现这些功能:get(index):获取链表中第 index 个节点的值。如果索引无效,则返...原创 2019-03-06 16:24:26 · 133 阅读 · 0 评论 -
617. Merge Two Binary Trees(合并二叉树)
题目描述Given 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 new binary ...原创 2019-03-06 15:49:50 · 129 阅读 · 0 评论 -
430. Flatten a Multilevel Doubly Linked List(扁平化多级双向链表)
题目描述您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表。这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例所示。扁平化列表,使所有结点出现在单级双链表中。您将获得列表第一级的头部。示例:输入:1—2---3—4---5—6–NULL|7—8---9—10–NULL|11–12–NULL输出:1-2-3-7...原创 2019-03-03 20:14:01 · 465 阅读 · 0 评论 -
100. Same Tree
题目描述Given 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 value.方法思路...原创 2019-03-19 15:58:30 · 112 阅读 · 0 评论 -
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 · 187 阅读 · 0 评论