auto commit
This commit is contained in:
@ -44,7 +44,9 @@
|
||||
|
||||
## 1. 树的高度
|
||||
|
||||
[104. Maximum Depth of Binary Tree (Easy)](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
|
||||
104\. Maximum Depth of Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/maximum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/)
|
||||
|
||||
```java
|
||||
public int maxDepth(TreeNode root) {
|
||||
@ -55,7 +57,9 @@ public int maxDepth(TreeNode root) {
|
||||
|
||||
## 2. 平衡树
|
||||
|
||||
[110. Balanced Binary Tree (Easy)](https://leetcode.com/problems/balanced-binary-tree/description/)
|
||||
110\. Balanced Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/balanced-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/balanced-binary-tree/description/)
|
||||
|
||||
```html
|
||||
3
|
||||
@ -86,7 +90,9 @@ public int maxDepth(TreeNode root) {
|
||||
|
||||
## 3. 两节点的最长路径
|
||||
|
||||
[543. Diameter of Binary Tree (Easy)](https://leetcode.com/problems/diameter-of-binary-tree/description/)
|
||||
543\. Diameter of Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/diameter-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/diameter-of-binary-tree/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -119,7 +125,9 @@ private int depth(TreeNode root) {
|
||||
|
||||
## 4. 翻转树
|
||||
|
||||
[226. Invert Binary Tree (Easy)](https://leetcode.com/problems/invert-binary-tree/description/)
|
||||
226\. Invert Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/invert-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/invert-binary-tree/description/)
|
||||
|
||||
```java
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
@ -133,7 +141,9 @@ public TreeNode invertTree(TreeNode root) {
|
||||
|
||||
## 5. 归并两棵树
|
||||
|
||||
[617. Merge Two Binary Trees (Easy)](https://leetcode.com/problems/merge-two-binary-trees/description/)
|
||||
617\. Merge Two Binary Trees (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/merge-two-binary-trees/description/) / [力扣](https://leetcode-cn.com/problems/merge-two-binary-trees/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -166,7 +176,9 @@ public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
|
||||
|
||||
## 6. 判断路径和是否等于一个数
|
||||
|
||||
[Leetcdoe : 112. Path Sum (Easy)](https://leetcode.com/problems/path-sum/description/)
|
||||
Leetcdoe : 112. Path Sum (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/path-sum/description/) / [力扣](https://leetcode-cn.com/problems/path-sum/description/)
|
||||
|
||||
```html
|
||||
Given the below binary tree and sum = 22,
|
||||
@ -194,7 +206,9 @@ public boolean hasPathSum(TreeNode root, int sum) {
|
||||
|
||||
## 7. 统计路径和等于一个数的路径数量
|
||||
|
||||
[437. Path Sum III (Easy)](https://leetcode.com/problems/path-sum-iii/description/)
|
||||
437\. Path Sum III (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/path-sum-iii/description/) / [力扣](https://leetcode-cn.com/problems/path-sum-iii/description/)
|
||||
|
||||
```html
|
||||
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
|
||||
@ -234,7 +248,9 @@ private int pathSumStartWithRoot(TreeNode root, int sum) {
|
||||
|
||||
## 8. 子树
|
||||
|
||||
[572. Subtree of Another Tree (Easy)](https://leetcode.com/problems/subtree-of-another-tree/description/)
|
||||
572\. Subtree of Another Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/subtree-of-another-tree/description/) / [力扣](https://leetcode-cn.com/problems/subtree-of-another-tree/description/)
|
||||
|
||||
```html
|
||||
Given tree s:
|
||||
@ -285,7 +301,9 @@ private boolean isSubtreeWithRoot(TreeNode s, TreeNode t) {
|
||||
|
||||
## 9. 树的对称
|
||||
|
||||
[101. Symmetric Tree (Easy)](https://leetcode.com/problems/symmetric-tree/description/)
|
||||
101\. Symmetric Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/symmetric-tree/description/) / [力扣](https://leetcode-cn.com/problems/symmetric-tree/description/)
|
||||
|
||||
```html
|
||||
1
|
||||
@ -311,7 +329,9 @@ private boolean isSymmetric(TreeNode t1, TreeNode t2) {
|
||||
|
||||
## 10. 最小路径
|
||||
|
||||
[111. Minimum Depth of Binary Tree (Easy)](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/)
|
||||
111\. Minimum Depth of Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/minimum-depth-of-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree/description/)
|
||||
|
||||
树的根节点到叶子节点的最小路径长度
|
||||
|
||||
@ -327,7 +347,9 @@ public int minDepth(TreeNode root) {
|
||||
|
||||
## 11. 统计左叶子节点的和
|
||||
|
||||
[404. Sum of Left Leaves (Easy)](https://leetcode.com/problems/sum-of-left-leaves/description/)
|
||||
404\. Sum of Left Leaves (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/sum-of-left-leaves/description/) / [力扣](https://leetcode-cn.com/problems/sum-of-left-leaves/description/)
|
||||
|
||||
```html
|
||||
3
|
||||
@ -354,7 +376,9 @@ private boolean isLeaf(TreeNode node){
|
||||
|
||||
## 12. 相同节点值的最大路径长度
|
||||
|
||||
[687. Longest Univalue Path (Easy)](https://leetcode.com/problems/longest-univalue-path/)
|
||||
687\. Longest Univalue Path (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/longest-univalue-path/) / [力扣](https://leetcode-cn.com/problems/longest-univalue-path/)
|
||||
|
||||
```html
|
||||
1
|
||||
@ -387,7 +411,9 @@ private int dfs(TreeNode root){
|
||||
|
||||
## 13. 间隔遍历
|
||||
|
||||
[337. House Robber III (Medium)](https://leetcode.com/problems/house-robber-iii/description/)
|
||||
337\. House Robber III (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/house-robber-iii/description/) / [力扣](https://leetcode-cn.com/problems/house-robber-iii/description/)
|
||||
|
||||
```html
|
||||
3
|
||||
@ -411,7 +437,9 @@ public int rob(TreeNode root) {
|
||||
|
||||
## 14. 找出二叉树中第二小的节点
|
||||
|
||||
[671. Second Minimum Node In a Binary Tree (Easy)](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/)
|
||||
671\. Second Minimum Node In a Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/second-minimum-node-in-a-binary-tree/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -446,7 +474,9 @@ public int findSecondMinimumValue(TreeNode root) {
|
||||
|
||||
## 1. 一棵树每层节点的平均数
|
||||
|
||||
[637. Average of Levels in Binary Tree (Easy)](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/)
|
||||
637\. Average of Levels in Binary Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/average-of-levels-in-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/average-of-levels-in-binary-tree/description/)
|
||||
|
||||
```java
|
||||
public List<Double> averageOfLevels(TreeNode root) {
|
||||
@ -471,7 +501,9 @@ public List<Double> averageOfLevels(TreeNode root) {
|
||||
|
||||
## 2. 得到左下角的节点
|
||||
|
||||
[513. Find Bottom Left Tree Value (Easy)](https://leetcode.com/problems/find-bottom-left-tree-value/description/)
|
||||
513\. Find Bottom Left Tree Value (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/find-bottom-left-tree-value/description/) / [力扣](https://leetcode-cn.com/problems/find-bottom-left-tree-value/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -552,7 +584,9 @@ void dfs(TreeNode root) {
|
||||
|
||||
## 1. 非递归实现二叉树的前序遍历
|
||||
|
||||
[144. Binary Tree Preorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-preorder-traversal/description/)
|
||||
144\. Binary Tree Preorder Traversal (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/binary-tree-preorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-preorder-traversal/description/)
|
||||
|
||||
```java
|
||||
public List<Integer> preorderTraversal(TreeNode root) {
|
||||
@ -572,7 +606,9 @@ public List<Integer> preorderTraversal(TreeNode root) {
|
||||
|
||||
## 2. 非递归实现二叉树的后序遍历
|
||||
|
||||
[145. Binary Tree Postorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-postorder-traversal/description/)
|
||||
145\. Binary Tree Postorder Traversal (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/binary-tree-postorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-postorder-traversal/description/)
|
||||
|
||||
前序遍历为 root -> left -> right,后序遍历为 left -> right -> root。可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反。
|
||||
|
||||
@ -595,7 +631,9 @@ public List<Integer> postorderTraversal(TreeNode root) {
|
||||
|
||||
## 3. 非递归实现二叉树的中序遍历
|
||||
|
||||
[94. Binary Tree Inorder Traversal (Medium)](https://leetcode.com/problems/binary-tree-inorder-traversal/description/)
|
||||
94\. Binary Tree Inorder Traversal (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/binary-tree-inorder-traversal/description/) / [力扣](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/description/)
|
||||
|
||||
```java
|
||||
public List<Integer> inorderTraversal(TreeNode root) {
|
||||
@ -624,7 +662,9 @@ public List<Integer> inorderTraversal(TreeNode root) {
|
||||
|
||||
## 1. 修剪二叉查找树
|
||||
|
||||
[669. Trim a Binary Search Tree (Easy)](https://leetcode.com/problems/trim-a-binary-search-tree/description/)
|
||||
669\. Trim a Binary Search Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/trim-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/trim-a-binary-search-tree/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -664,7 +704,9 @@ public TreeNode trimBST(TreeNode root, int L, int R) {
|
||||
|
||||
## 2. 寻找二叉查找树的第 k 个元素
|
||||
|
||||
[230. Kth Smallest Element in a BST (Medium)](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/)
|
||||
230\. Kth Smallest Element in a BST (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/kth-smallest-element-in-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/description/)
|
||||
|
||||
|
||||
中序遍历解法:
|
||||
@ -708,7 +750,9 @@ private int count(TreeNode node) {
|
||||
|
||||
## 3. 把二叉查找树每个节点的值都加上比它大的节点的值
|
||||
|
||||
[Convert BST to Greater Tree (Easy)](https://leetcode.com/problems/convert-bst-to-greater-tree/description/)
|
||||
Convert BST to Greater Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-bst-to-greater-tree/description/)
|
||||
|
||||
```html
|
||||
Input: The root of a Binary Search Tree like this:
|
||||
@ -745,7 +789,9 @@ private void traver(TreeNode node) {
|
||||
|
||||
## 4. 二叉查找树的最近公共祖先
|
||||
|
||||
[235. Lowest Common Ancestor of a Binary Search Tree (Easy)](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
|
||||
235\. Lowest Common Ancestor of a Binary Search Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-search-tree/description/)
|
||||
|
||||
```html
|
||||
_______6______
|
||||
@ -769,7 +815,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
|
||||
## 5. 二叉树的最近公共祖先
|
||||
|
||||
[236. Lowest Common Ancestor of a Binary Tree (Medium) ](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
|
||||
236\. Lowest Common Ancestor of a Binary Tree (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/) / [力扣](https://leetcode-cn.com/problems/lowest-common-ancestor-of-a-binary-tree/description/)
|
||||
|
||||
```html
|
||||
_______3______
|
||||
@ -794,7 +842,9 @@ public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
|
||||
|
||||
## 6. 从有序数组中构造二叉查找树
|
||||
|
||||
[108. Convert Sorted Array to Binary Search Tree (Easy)](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
||||
108\. Convert Sorted Array to Binary Search Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-array-to-binary-search-tree/description/)
|
||||
|
||||
```java
|
||||
public TreeNode sortedArrayToBST(int[] nums) {
|
||||
@ -813,7 +863,9 @@ private TreeNode toBST(int[] nums, int sIdx, int eIdx){
|
||||
|
||||
## 7. 根据有序链表构造平衡的二叉查找树
|
||||
|
||||
[109. Convert Sorted List to Binary Search Tree (Medium)](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/)
|
||||
109\. Convert Sorted List to Binary Search Tree (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/convert-sorted-list-to-binary-search-tree/description/)
|
||||
|
||||
```html
|
||||
Given the sorted linked list: [-10,-3,0,5,9],
|
||||
@ -854,7 +906,9 @@ private ListNode preMid(ListNode head) {
|
||||
|
||||
## 8. 在二叉查找树中寻找两个节点,使它们的和为一个给定值
|
||||
|
||||
[653. Two Sum IV - Input is a BST (Easy)](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/)
|
||||
653\. Two Sum IV - Input is a BST (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/two-sum-iv-input-is-a-bst/description/) / [力扣](https://leetcode-cn.com/problems/two-sum-iv-input-is-a-bst/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -898,7 +952,9 @@ private void inOrder(TreeNode root, List<Integer> nums) {
|
||||
|
||||
## 9. 在二叉查找树中查找两个节点之差的最小绝对值
|
||||
|
||||
[530. Minimum Absolute Difference in BST (Easy)](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/)
|
||||
530\. Minimum Absolute Difference in BST (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/minimum-absolute-difference-in-bst/description/) / [力扣](https://leetcode-cn.com/problems/minimum-absolute-difference-in-bst/description/)
|
||||
|
||||
```html
|
||||
Input:
|
||||
@ -936,7 +992,9 @@ private void inOrder(TreeNode node) {
|
||||
|
||||
## 10. 寻找二叉查找树中出现次数最多的值
|
||||
|
||||
[501. Find Mode in Binary Search Tree (Easy)](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/)
|
||||
501\. Find Mode in Binary Search Tree (Easy)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/find-mode-in-binary-search-tree/description/) / [力扣](https://leetcode-cn.com/problems/find-mode-in-binary-search-tree/description/)
|
||||
|
||||
```html
|
||||
1
|
||||
@ -993,7 +1051,9 @@ Trie,又称前缀树或字典树,用于判断字符串是否存在或者是
|
||||
|
||||
## 1. 实现一个 Trie
|
||||
|
||||
[208. Implement Trie (Prefix Tree) (Medium)](https://leetcode.com/problems/implement-trie-prefix-tree/description/)
|
||||
208\. Implement Trie (Prefix Tree) (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/implement-trie-prefix-tree/description/) / [力扣](https://leetcode-cn.com/problems/implement-trie-prefix-tree/description/)
|
||||
|
||||
```java
|
||||
class Trie {
|
||||
@ -1055,7 +1115,9 @@ class Trie {
|
||||
|
||||
## 2. 实现一个 Trie,用来求前缀和
|
||||
|
||||
[677. Map Sum Pairs (Medium)](https://leetcode.com/problems/map-sum-pairs/description/)
|
||||
677\. Map Sum Pairs (Medium)
|
||||
|
||||
[Leetcode](https://leetcode.com/problems/map-sum-pairs/description/) / [力扣](https://leetcode-cn.com/problems/map-sum-pairs/description/)
|
||||
|
||||
```html
|
||||
Input: insert("apple", 3), Output: Null
|
||||
|
Reference in New Issue
Block a user