Tree
N.C_IPOC_BUPT
一个喜欢科怀.莱昂纳德的准程序猿,研一在读。
展开
-
687. Longest Univalue Path(最长同值路径)
题目描述Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root.Note: The length of path between two nodes...转载 2019-03-17 12:34:19 · 132 阅读 · 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 · 127 阅读 · 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 · 92 阅读 · 0 评论 -
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 · 150 阅读 · 0 评论 -
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 · 98 阅读 · 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 · 97 阅读 · 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 · 132 阅读 · 0 评论 -
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 · 111 阅读 · 0 评论 -
538. Convert BST to Greater Tree
题目描述方法思路Approach1: recursive + inorder traversalclass Solution { //Runtime: 10 ms, faster than 28.05% //Memory Usage: 39.6 MB, less than 71.62% Stack<TreeNode> stk; public Tre...原创 2019-03-18 17:46:00 · 71 阅读 · 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 · 118 阅读 · 0 评论 -
653. Two Sum IV - Input is a BST
题目描述Given 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.方法思路这道题目相对简单,只要遍历了数组并进行查找即可,相比较而言,中序遍历要比先序遍历...原创 2019-03-18 16:15:28 · 133 阅读 · 0 评论 -
993. Cousins in Binary Tree
题目描述方法思路基本思路就是使用深度优先搜索Approach1: based on Mapsclass Solution { //Runtime: 4 ms, faster than 79.97% //Memory Usage: 37.2 MB, less than 45.22% Map<Integer, Integer> depth; Map...原创 2019-03-17 14:36:28 · 128 阅读 · 0 评论 -
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 · 93 阅读 · 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 · 89 阅读 · 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 · 105 阅读 · 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 · 139 阅读 · 0 评论 -
230. Kth Smallest Element in a BST
题目描述Given 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.方法思路求一组数中的第K个最小值是类非常经典的题目,...原创 2019-03-13 11:05:45 · 120 阅读 · 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 · 143 阅读 · 0 评论 -
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 · 156 阅读 · 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 · 220 阅读 · 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 · 124 阅读 · 0 评论 -
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 · 95 阅读 · 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 · 163 阅读 · 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 · 135 阅读 · 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 · 179 阅读 · 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 · 181 阅读 · 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 · 144 阅读 · 0 评论 -
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 · 176 阅读 · 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 · 194 阅读 · 0 评论 -
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 · 147 阅读 · 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 · 171 阅读 · 0 评论 -
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 · 230 阅读 · 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 · 109 阅读 · 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 · 101 阅读 · 0 评论 -
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 · 88 阅读 · 0 评论 -
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 · 90 阅读 · 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 · 127 阅读 · 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 · 278 阅读 · 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 · 109 阅读 · 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 · 130 阅读 · 0 评论