我的Leetcode专栏
这个专栏用于存放和展示我的刷题笔记。
N.C_IPOC_BUPT
一个喜欢科怀.莱昂纳德的准程序猿,研一在读。
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
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 · 134 阅读 · 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 · 181 阅读 · 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 · 224 阅读 · 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 · 142 阅读 · 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 · 194 阅读 · 0 评论 -
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 · 208 阅读 · 0 评论 -
437. Path Sum III
题目描述You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it ...原创 2019-03-16 12:04:01 · 153 阅读 · 0 评论 -
257. Binary Tree Paths
题目描述方法思路class Solution { //Runtime: 6 ms, faster than 100.00% //Memory Usage: 38.7 MB, less than 18.78% public List<String> binaryTreePaths(TreeNode root){ List<String...原创 2019-03-16 11:24:23 · 175 阅读 · 0 评论 -
113. Path Sum II
题目描述方法思路搜索方法为深度优先搜索,遍历顺序为先序遍历。本题中,因为满足要求的路径并不唯一,需要用回溯的思想来解决。简述思路如下:1、先序遍历,从根节点开始添加,然后搜索左子树,一直达到叶子节点,得到根叶路径5-4-11–7,检验是否满足要求。2、随后进行回溯,从队列尾部删除一个数据,返回到根结点11,然后搜索其右子树,根叶路径为5-4-11-2,检验是否满足要求。3、如此反复...原创 2019-03-16 10:17:02 · 183 阅读 · 0 评论 -
112. Path Sum
题目描述方法思路class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 38.6 MB, less than 51.70% public boolean hasPathSum(TreeNode root, int sum) { if(root == null) retu...原创 2019-03-15 11:36:56 · 151 阅读 · 0 评论 -
1008. Construct Binary Search Tree from Preorder Traversal(根据先序遍历构建BST)
题目描述Return the root node of a binary search tree that matches the given preorder traversal.(Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a ...原创 2019-03-15 10:49:57 · 292 阅读 · 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 · 240 阅读 · 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 · 190 阅读 · 0 评论 -
701. Insert into a Binary Search Tree(在二叉搜索树中插入节点)
题目描述Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed th...原创 2019-03-14 11:31:05 · 245 阅读 · 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 · 366 阅读 · 0 评论 -
654. Maximum Binary Tree(最大二叉树)
题目描述方法思路class Solution { //Runtime: 5 ms, faster than 100.00% //Memory Usage: 39.9 MB, less than 56.46% public TreeNode constructMaximumBinaryTree(int[] nums) { TreeNode root = ...原创 2019-03-14 11:24:19 · 233 阅读 · 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 · 283 阅读 · 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 · 231 阅读 · 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 · 204 阅读 · 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 · 219 阅读 · 0 评论 -
637. Average of Levels in Binary Tree
题目描述方法思路Approach1:class Solution { //Runtime: 4 ms, faster than 99.22% of Java //Memory Usage: 42.1 MB, less than 62.80% public List<Double> averageOfLevels(TreeNode root) { ...原创 2019-03-12 12:24:14 · 185 阅读 · 0 评论 -
107. Binary Tree Level Order Traversal II(二叉树的层序遍历II)
题目描述方法思路class Solution { //Runtime: 1 ms, faster than 92.29% //Memory Usage: 37.2 MB, less than 74.03% public List<List<Integer>> levelOrderBottom(TreeNode root) { L...原创 2019-03-12 12:21:37 · 176 阅读 · 0 评论 -
114. Flatten Binary Tree to Linked List
题目描述方法思路Approach1:class Solution{ //Runtime: 6 ms, faster than 99.92% //Memory Usage: 39.2 MB, less than 5.09% private TreeNode next = null; public void flatten(TreeNode root) { if...原创 2019-03-12 12:19:51 · 180 阅读 · 0 评论 -
226. Invert Binary Tree(反转二叉树)
题目描述方法思路Approach1:class Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 35.8 MB, less than 9.58% public TreeNode invertTree(TreeNode root) { if(root == null) r...原创 2019-03-12 12:16:29 · 268 阅读 · 0 评论 -
919. Complete Binary Tree Inserter(完全二叉树插入器)
题目描述对这种题没有任何的抵抗力,只能乖乖地看Solution完全二叉树是每一层(除最后一层外)都是完全填充(即,结点数达到最大)的,并且所有的结点都尽可能地集中在左侧。设计一个用完全二叉树初始化的数据结构 CBTInserter,它支持以下几种操作:CBTInserter(TreeNode root) 使用头结点为 root 的给定树初始化该数据结构;CBTInserter.inser...原创 2019-03-11 19:35:24 · 305 阅读 · 0 评论 -
94. Binary Tree Inorder Traversal(二叉树的中序遍历)
题目描述方法思路Approach1: recursiveclass Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 36.3 MB, less than 11.58% List&lt;Integer&gt; res = new LinkedList&lt;&gt;(); publ...原创 2019-03-11 19:08:21 · 164 阅读 · 0 评论 -
144. Binary Tree Preorder Traversal
题目描述Given a binary tree, return the preorder traversal of its nodes’ values.方法思路Approach1: 递归 recursiveclass Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 36.3 MB, less ...原创 2019-03-11 18:06:35 · 225 阅读 · 0 评论 -
145. Binary Tree Postorder Traversal
题目描述Given a binary tree, return the postorder traversal of its nodes’ values.方法思路Approach1: recursiveclass Solution { //Runtime: 0 ms, faster than 100.00% //Memory Usage: 36.2 MB, less th...原创 2019-03-11 17:49:33 · 204 阅读 · 0 评论 -
669. Trim a Binary Search Tree(修剪搜索二叉树)
题目描述Given 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 tree, so t...原创 2019-03-11 16:53:19 · 197 阅读 · 0 评论 -
75. Sort Colors
题目描述Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use th...原创 2019-03-01 12:49:13 · 123 阅读 · 0 评论 -
Leetcode 31. Next Permutation(Array)
Leetcode 31. Next Permutation(Array)DescriptionImplement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possibl...原创 2019-01-28 18:18:25 · 273 阅读 · 0 评论 -
429. N-ary Tree Level Order Traversal(N叉树的层序遍历)
题目描述Given an n-ary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level).For example, given a 3-ary tree:方法思路class Solution { //Runtime: 5 ms, ...原创 2019-03-10 12:00:19 · 216 阅读 · 0 评论 -
106. Construct Binary Tree from Inorder and Postorder Traversal(由中序遍历和后序遍历构建二叉树)
题目描述Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.For example, given方法思路由中序遍历和后序遍历确定二叉树0.确定递归返回的临界条件。...原创 2019-03-10 11:17:55 · 189 阅读 · 0 评论 -
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.方法思路由中序遍历和先序遍历确定二叉树0.确定递归返回的临界条件。if(start &amp;amp;amp;gt; end) r...原创 2019-03-10 11:08:01 · 431 阅读 · 0 评论 -
897. Increasing Order Search Tree
题目描述Given a tree, rearrange the tree in in-order so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only 1 right child.方法思路Approach1:class So...原创 2019-03-09 18:20:29 · 199 阅读 · 0 评论 -
872. Leaf-Similar Trees
题目描述Consider all the leaves of a binary tree. From left to right order, the values of those leaves form a leaf value sequence.For example, in the given tree above, the leaf value sequence is (6, 7...原创 2019-03-09 18:14:58 · 206 阅读 · 0 评论 -
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,null,null,15,7],方法思路class Solution ...原创 2019-03-08 12:31:03 · 324 阅读 · 0 评论 -
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 di...原创 2019-03-08 11:52:31 · 225 阅读 · 0 评论 -
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.Note: A leaf is a node with no chil...原创 2019-03-08 11:20:56 · 109 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree(二叉树的最大深度)
题目描述方法思路Approach1: recursiveclass Solution { public int maxDepth(TreeNode root) { if(root == null) return 0; int depth = 0; int left_depth = maxDepth(root.left); ...原创 2019-03-08 10:22:41 · 117 阅读 · 0 评论
分享