- 博客(60)
- 资源 (1)
- 收藏
- 关注
原创 二叉树的最近公共祖先
题意:给定一个二叉树, 找到该树中两个指定节点的最近公共祖先。百度百科中最近公共祖先的定义为:“对于有根树 T 的两个结点 p、q,最近公共祖先表示为一个结点 x,满足 x 是 p、q 的祖先且 x 的深度尽可能大(一个节点也可以是它自己的祖先)。”思路:从根递归遍历,1.如果根为空,返回空。2.如果根不空,则如果p或q等于根,返回根。3.如果p或q不为根,则遍历左右子树,如果左子树为空,则返回右子树,如果右子树为空,则返回左子树,如果都不空,则返回根,如果都空,则返回空。Tr..
2020-05-20 15:25:52
201
原创 二叉树的层序遍历
使用队列维护,时间复杂度O(n),可解决右视图问题vector<int> level(TreeNode* root) { if(root == NULL) return {}; vector<int> ans; queue<TreeNode*> q; q.push_back(root); while(!q.empty()) { int len = q.size(); for(int i=0; i&l
2020-05-19 20:21:28
246
原创 Leetcode 687. Longest Univalue Path
Leetcode 687. Longest Univalue PathGiven 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.The length o...
2020-02-16 15:42:41
171
原创 Leetcode 111. Minimum Depth of Binary Tree
Leetcode 111. Minimum Depth of Binary TreeGiven 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...
2020-02-16 12:28:01
133
原创 Leetcode 110. Balanced Binary Tree
Leetcode 110. Balanced Binary TreeGiven 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 left and right subtr...
2020-02-16 12:06:29
233
1
原创 Leetcode 671. Second Minimum Node In a Binary Tree
Leetcode 671. Second Minimum Node In a Binary TreeGiven a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node....
2020-02-15 23:49:32
187
原创 Leetcode 572. Subtree of Another Tree
Leetcode 572. Subtree of Another TreeGiven 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 consis...
2020-02-15 15:39:57
117
原创 Leetcode 437. Path Sum III
Leetcode 437. Path Sum IIIYou 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 roo...
2020-02-15 14:37:57
135
原创 Leetcode 101. Symmetric Tree
Leetcode 101. Symmetric TreeGiven a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric; But [1,2,2,n...
2020-02-15 12:23:24
119
原创 Leetcode 543. Diameter of Binary Tree
Leetcode 543. Diameter of Binary TreeGiven a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two n...
2020-02-15 11:39:48
120
原创 Leetcode 404. Sum of Left Leaves
Leetcode 404. Sum of Left LeavesFind the sum of all left leaves in a given binary tree.题目大意:返回给定的二叉树的所有左叶子结点的和。解题思路:判断是否为左叶子结点,是则累加。采用递归方式实现。代码:class Solution {public: int s...
2020-02-14 19:24:06
131
原创 Leetcode 226. Invert Binary Tree
Leetcode 226. Invert Binary TreeInvert a binary tree.题目大意:置换一棵二叉树,每个结点的左结点和右结点交换。解题思路:二叉树递归,设置中间值实现两值交换。时间复杂度O(n+m),其中n为结点数,m为边数。代码:class Solution {public: TreeNode* invertT...
2020-02-13 23:44:48
136
原创 Leetcode 104. Maximum Depth of Binary Tree
Leetcode 104. Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node...
2020-02-13 21:38:33
90
原创 Leetcode 617. Merge Two Binary Trees
Leetcode 617. Merge Two Binary TreesGiven 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 t...
2020-02-13 21:28:16
180
原创 Leetcode 279. Perfect Squares
Leetcode 279. Perfect SquaresGiven a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, …) which sum to n.Example 1:Input: n = 12Output: 3Explanation: ...
2020-02-12 17:42:24
142
原创 Leetcode 95. Unique Binary Search Trees II
Leetcode 95. Unique Binary Search Trees IIGiven an integer n, generate all structurally unique BST’s (binary search trees) that store values 1 … n.Example:Input: 3Output:[[1,null,3,2],[3,2,null...
2020-02-12 11:13:49
122
原创 Leetcode 241. Different Ways to Add Parentheses
Leetcode 241. Different Ways to Add ParenthesesGiven a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The ...
2020-02-11 20:20:19
172
原创 Leetcode 34. Find First and Last Position of Element in Sorted Array
Leetcode 34. Find First and Last Position of Element in Sorted ArrayGiven an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.Your algor...
2020-02-11 17:42:22
92
原创 Leetcode 153. Find Minimum in Rotated Sorted Array
Leetcode 153. Find Minimum in Rotated Sorted ArraySuppose an array sorted in ascending order 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])....
2020-02-11 10:56:27
99
原创 Leetcode 540. Single Element in a Sorted Array
Leetcode 540. Single Element in a Sorted ArrayYou are given a sorted array consisting of only integers where every element appears exactly twice, except for one element which appears exactly once. Fi...
2020-02-10 23:33:41
169
原创 Leetcode 633. Sum of Square Numbers
Leetcode 633. Sum of Square NumbersGiven a non-negative integer c, your task is to decide whether there’re two integers a and b such that aa + bb = c.Example 1:Input: 5Output: TrueExplanation: 1 ...
2020-02-09 16:07:47
125
原创 Leetcode 278. First Bad Version
Leetcode 278. First Bad VersionYou are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each v...
2020-02-09 15:29:46
150
原创 Leetcode 69. Sqrt(x)
Leetcode 69. Sqrt(x)Implement int sqrt(int x).Compute and return the square root of x, where x is guaranteed to be a non-negative integer.Since the return type is an integer, the decimal digits are t...
2020-02-09 13:38:55
123
原创 Leetcode 744. Find Smallest Letter Greater Than Target
Leetcode 744. Find Smallest Letter Greater Than TargetGiven a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the l...
2020-02-09 12:36:01
139
原创 Leetcode 55. Jump Game
Leetcode 55. Jump GameGiven an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that posit...
2020-02-08 23:39:59
94
原创 Leetcode 134. Gas Station
Leetcode 134. Gas StationThere are N gas stations along a circular route, where the amount of gas at station i is gas[i].You have a car with an unlimited gas tank and it costs cost[i] of gas to trave...
2020-02-08 16:25:20
113
原创 Leetcode 435. Non-overlapping Intervals
Leetcode 435. Non-overlapping IntervalsGiven a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping.Example 1:Input: [[...
2020-02-08 15:06:47
117
原创 Leetcode 452. Minimum Number of Arrows to Burst Balloons
Leetcode 452. Minimum Number of Arrows to Burst BalloonsThere are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of ...
2020-02-07 23:50:20
91
原创 Leetcode 406. Queue Reconstruction by Height
Leetcode 406. Queue Reconstruction by HeightSuppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person an...
2020-02-07 20:13:27
181
原创 Leetcode 763. Partition Labels
Leetcode 763. Partition LabelsA string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a l...
2020-02-07 16:44:46
150
原创 Leetcode 860. Lemonade Change
Leetcode 860. Lemonade ChangeAt a lemonade stand, each lemonade costs $5. Customers are standing in a queue to buy from you, and order one at a time (in the order specified by bills).Each customer wi...
2020-02-06 18:12:19
251
1
原创 Leetcode 665. Non-decreasing Array
Leetcode 665. Non-decreasing ArrayGiven an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[...
2020-02-06 17:57:48
97
原创 Leetcode 605. Can Place Flowers
Leetcode 605. Can Place FlowersSuppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for...
2020-02-05 23:30:50
102
原创 Leetcode 455. Assign Cookies
Leetcode 455. Assign CookiesAssume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor gi, which...
2020-02-05 21:19:00
147
原创 Leetcode 392. Is Subsequence
Leetcode 392. Is SubsequenceGiven a string s and a string t, check if s is subsequence of t.You may assume that there is only lower case English letters in both s and t. t is potentially a very long ...
2020-02-05 20:37:39
133
原创 Leetcode 122. Best Time to Buy and Sell Stock II
Leetcode 122. Best Time to Buy and Sell Stock IISay you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete...
2020-02-05 19:38:06
125
原创 Leetcode 121. Best Time to Buy and Sell Stock
Leetcode 121. Best Time to Buy and Sell StockSay you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (i.e...
2020-02-05 19:22:25
102
原创 Leetcode 215. Kth Largest Element in an Array
Leetcode 215. Kth Largest Element in an ArrayFind the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.Example 1:I...
2020-02-04 23:06:59
94
原创 Leetcode 451. Sort Characters By Frequency
Leetcode 451. Sort Characters By FrequencyGiven a string, sort it in decreasing order based on the frequency of characters.Example 1:Input:“tree”Output:“eert”Explanation:‘e’ appears twice whil...
2020-02-04 21:02:03
218
原创 Leetcode 347. Top K Frequent Elements
Leetcode 347. Top K Frequent ElementsGiven a non-empty array of integers, return the k most frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]Example 2:Input: nums = [1]...
2020-02-04 20:28:12
118
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人