auto commit
This commit is contained in:
parent
9e4c3d79b5
commit
43cee1d82b
@ -36,7 +36,7 @@
|
|||||||
* [哈希表](#哈希表)
|
* [哈希表](#哈希表)
|
||||||
* [字符串](#字符串)
|
* [字符串](#字符串)
|
||||||
* [数组与矩阵](#数组与矩阵)
|
* [数组与矩阵](#数组与矩阵)
|
||||||
* [1\~n 分布](#1\~n-分布)
|
* [1-n 分布](#1-n-分布)
|
||||||
* [有序矩阵](#有序矩阵)
|
* [有序矩阵](#有序矩阵)
|
||||||
* [链表](#链表)
|
* [链表](#链表)
|
||||||
* [树](#树)
|
* [树](#树)
|
||||||
@ -3472,7 +3472,7 @@ public void moveZeroes(int[] nums) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1\~n 分布
|
### 1-n 分布
|
||||||
|
|
||||||
**一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出丢失的数和重复的数**
|
**一个数组元素在 [1, n] 之间,其中一个数被替换为另一个数,找出丢失的数和重复的数**
|
||||||
|
|
||||||
@ -3855,6 +3855,23 @@ public TreeNode invertTree(TreeNode root) {
|
|||||||
|
|
||||||
[Leetcode : 617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
|
[Leetcode : 617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
Input:
|
||||||
|
Tree 1 Tree 2
|
||||||
|
1 2
|
||||||
|
/ \ / \
|
||||||
|
3 2 1 3
|
||||||
|
/ \ \
|
||||||
|
5 4 7
|
||||||
|
Output:
|
||||||
|
Merged tree:
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
4 5
|
||||||
|
/ \ \
|
||||||
|
5 4 7
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
|
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
|
||||||
if(t1 == null && t2 == null) return null;
|
if(t1 == null && t2 == null) return null;
|
||||||
@ -3871,7 +3888,19 @@ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
|
|||||||
|
|
||||||
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
|
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
|
||||||
|
|
||||||
题目描述:路径和定义为从 root 到 leaf 的所有节点的和
|
```html
|
||||||
|
Given the below binary tree and sum = 22,
|
||||||
|
5
|
||||||
|
/ \
|
||||||
|
4 8
|
||||||
|
/ / \
|
||||||
|
11 13 4
|
||||||
|
/ \ \
|
||||||
|
7 2 1
|
||||||
|
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.
|
||||||
|
```
|
||||||
|
|
||||||
|
路径和定义为从 root 到 leaf 的所有节点的和
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public boolean hasPathSum(TreeNode root, int sum) {
|
public boolean hasPathSum(TreeNode root, int sum) {
|
||||||
@ -3885,9 +3914,25 @@ public boolean hasPathSum(TreeNode root, int sum) {
|
|||||||
|
|
||||||
[Leetcode : 437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
|
[Leetcode : 437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
|
||||||
|
|
||||||
题目描述:路径不一定以 root 开头并以 leaf 结尾,但是必须连续
|
```html
|
||||||
|
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
|
||||||
|
|
||||||
pathSumStartWithRoot() 方法统计以某个节点开头的路径个数。
|
10
|
||||||
|
/ \
|
||||||
|
5 -3
|
||||||
|
/ \ \
|
||||||
|
3 2 11
|
||||||
|
/ \ \
|
||||||
|
3 -2 1
|
||||||
|
|
||||||
|
Return 3. The paths that sum to 8 are:
|
||||||
|
|
||||||
|
1. 5 -> 3
|
||||||
|
2. 5 -> 2 -> 1
|
||||||
|
3. -3 -> 11
|
||||||
|
```
|
||||||
|
|
||||||
|
路径不一定以 root 开头并以 leaf 结尾,但是必须连续
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int pathSum(TreeNode root, int sum) {
|
public int pathSum(TreeNode root, int sum) {
|
||||||
@ -3909,6 +3954,14 @@ private int pathSumStartWithRoot(TreeNode root, int sum){
|
|||||||
|
|
||||||
[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
|
[Leetcode : 101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
1
|
||||||
|
/ \
|
||||||
|
2 2
|
||||||
|
/ \ / \
|
||||||
|
3 4 4 3
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public boolean isSymmetric(TreeNode root) {
|
public boolean isSymmetric(TreeNode root) {
|
||||||
if(root == null) return true;
|
if(root == null) return true;
|
||||||
@ -3927,7 +3980,15 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2){
|
|||||||
|
|
||||||
[Leetcode : 110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
|
[Leetcode : 110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
|
||||||
|
|
||||||
题目描述:左右子树高度差是否都小于等于 1
|
```html
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
9 20
|
||||||
|
/ \
|
||||||
|
15 7
|
||||||
|
```
|
||||||
|
|
||||||
|
平衡树左右子树高度差都小于等于 1
|
||||||
|
|
||||||
```java
|
```java
|
||||||
private boolean result = true;
|
private boolean result = true;
|
||||||
@ -3950,7 +4011,7 @@ public int maxDepth(TreeNode root) {
|
|||||||
|
|
||||||
[Leetcode : 111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
|
[Leetcode : 111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
|
||||||
|
|
||||||
题目描述:树的根节点到叶子节点的最小长度
|
树的根节点到叶子节点的最小路径长度
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int minDepth(TreeNode root) {
|
public int minDepth(TreeNode root) {
|
||||||
@ -3966,24 +4027,63 @@ public int minDepth(TreeNode root) {
|
|||||||
|
|
||||||
[Leetcode : 404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
|
[Leetcode : 404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
9 20
|
||||||
|
/ \
|
||||||
|
15 7
|
||||||
|
|
||||||
|
There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public int sumOfLeftLeaves(TreeNode root) {
|
public int sumOfLeftLeaves(TreeNode root) {
|
||||||
if(root == null) return 0;
|
if(root == null) {
|
||||||
if(isLeaf(root.left)) return root.left.val + sumOfLeftLeaves(root.right);
|
return 0;
|
||||||
|
}
|
||||||
|
if(isLeaf(root.left)) {
|
||||||
|
return root.left.val + sumOfLeftLeaves(root.right);
|
||||||
|
}
|
||||||
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
|
return sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isLeaf(TreeNode node){
|
private boolean isLeaf(TreeNode node){
|
||||||
if(node == null) return false;
|
if(node == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
return node.left == null && node.right == null;
|
return node.left == null && node.right == null;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**修剪一棵树**
|
**修剪二叉查找树**
|
||||||
|
|
||||||
[Leetcode : 669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
|
[Leetcode : 669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
|
||||||
|
|
||||||
题目描述:只保留值在 L \~ R 之间的节点
|
```html
|
||||||
|
Input:
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
0 4
|
||||||
|
\
|
||||||
|
2
|
||||||
|
/
|
||||||
|
1
|
||||||
|
|
||||||
|
L = 1
|
||||||
|
R = 3
|
||||||
|
|
||||||
|
Output:
|
||||||
|
3
|
||||||
|
/
|
||||||
|
2
|
||||||
|
/
|
||||||
|
1
|
||||||
|
```
|
||||||
|
|
||||||
|
二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。
|
||||||
|
|
||||||
|
只保留值在 L \~ R 之间的节点
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode trimBST(TreeNode root, int L, int R) {
|
public TreeNode trimBST(TreeNode root, int L, int R) {
|
||||||
@ -4000,6 +4100,20 @@ public TreeNode trimBST(TreeNode root, int L, int R) {
|
|||||||
|
|
||||||
[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
|
[Leetcode : 572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
Given tree s:
|
||||||
|
3
|
||||||
|
/ \
|
||||||
|
4 5
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
Given tree t:
|
||||||
|
4
|
||||||
|
/ \
|
||||||
|
1 2
|
||||||
|
Return true, because t has the same structure and node values with a subtree of s.
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public boolean isSubtree(TreeNode s, TreeNode t) {
|
public boolean isSubtree(TreeNode s, TreeNode t) {
|
||||||
if(s == null && t == null) return true;
|
if(s == null && t == null) return true;
|
||||||
@ -4020,8 +4134,6 @@ private boolean isSame(TreeNode s, TreeNode t){
|
|||||||
|
|
||||||
[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
[Leetcode : 108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
||||||
|
|
||||||
二叉查找树(BST):根节点大于等于左子树所有节点,小于等于右子树所有节点。
|
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode sortedArrayToBST(int[] nums) {
|
public TreeNode sortedArrayToBST(int[] nums) {
|
||||||
return toBST(nums, 0, nums.length - 1);
|
return toBST(nums, 0, nums.length - 1);
|
||||||
@ -4040,6 +4152,8 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
|
|||||||
**两节点的最长路径**
|
**两节点的最长路径**
|
||||||
|
|
||||||
```html
|
```html
|
||||||
|
Input:
|
||||||
|
|
||||||
1
|
1
|
||||||
/ \
|
/ \
|
||||||
2 3
|
2 3
|
||||||
@ -4049,6 +4163,8 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
|
|||||||
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
|
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
|
||||||
```
|
```
|
||||||
|
|
||||||
|
[Leetcode : 543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/)
|
||||||
|
|
||||||
```java
|
```java
|
||||||
private int max = 0;
|
private int max = 0;
|
||||||
|
|
||||||
@ -4058,9 +4174,7 @@ public int diameterOfBinaryTree(TreeNode root) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private int depth(TreeNode root) {
|
private int depth(TreeNode root) {
|
||||||
if (root == null) {
|
if (root == null) return 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
int leftDepth = depth(root.left);
|
int leftDepth = depth(root.left);
|
||||||
int rightDepth = depth(root.right);
|
int rightDepth = depth(root.right);
|
||||||
max = Math.max(max, leftDepth + rightDepth);
|
max = Math.max(max, leftDepth + rightDepth);
|
||||||
@ -4099,10 +4213,21 @@ public int findSecondMinimumValue(TreeNode root) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**寻找两个节点的最近公共祖先**
|
**二叉查找树的最近公共祖先**
|
||||||
|
|
||||||
[Leetcode : 235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
|
[Leetcode : 235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
_______6______
|
||||||
|
/ \
|
||||||
|
___2__ ___8__
|
||||||
|
/ \ / \
|
||||||
|
0 _4 7 9
|
||||||
|
/ \
|
||||||
|
3 5
|
||||||
|
For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
|
if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q);
|
||||||
@ -4111,10 +4236,21 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
**最近公共祖先**
|
**二叉树的最近公共祖先**
|
||||||
|
|
||||||
[Leetcode : 236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
|
[Leetcode : 236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
|
||||||
|
|
||||||
|
```html
|
||||||
|
_______3______
|
||||||
|
/ \
|
||||||
|
___5__ ___1__
|
||||||
|
/ \ / \
|
||||||
|
6 _2 0 8
|
||||||
|
/ \
|
||||||
|
7 4
|
||||||
|
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
|
||||||
|
```
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||||
if (root == null || root == p || root == q) return root;
|
if (root == null || root == p || root == q) return root;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user