-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
256 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 第六章 二叉树part03 | ||
|
||
今日内容: | ||
|
||
● 104.二叉树的最大深度 559.n叉树的最大深度 | ||
● 111.二叉树的最小深度 | ||
● 222.完全二叉树的节点个数 | ||
|
||
迭代法,大家可以直接过,二刷有精力的时候 再去掌握迭代法。 | ||
|
||
详细布置 | ||
|
||
## 104.二叉树的最大深度 (优先掌握递归) | ||
|
||
什么是深度,什么是高度,如何求深度,如何求高度,这里有关系到二叉树的遍历方式。 | ||
|
||
大家 要先看视频讲解,就知道以上我说的内容了,很多录友刷过这道题,但理解的还不够。 | ||
|
||
题目链接/文章讲解/视频讲解: https://programmercarl.com/0104.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%A4%A7%E6%B7%B1%E5%BA%A6.html | ||
|
||
## 111.二叉树的最小深度 (优先掌握递归) | ||
|
||
先看视频讲解,和最大深度 看似差不多,其实 差距还挺大,有坑。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0111.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%9C%80%E5%B0%8F%E6%B7%B1%E5%BA%A6.html | ||
|
||
|
||
## 222.完全二叉树的节点个数(优先掌握递归) | ||
|
||
需要了解,普通二叉树 怎么求,完全二叉树又怎么求 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0222.%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AA%E6%95%B0.html | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# 104. 二叉树的最大深度 | ||
## 题目描述 | ||
|
||
给定一个二叉树,找出其最大深度。 | ||
|
||
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。 | ||
|
||
说明: 叶子节点是指没有子节点的节点。 | ||
|
||
示例: | ||
给定二叉树 [3,9,20,null,null,15,7], | ||
|
||
## 解题思路 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int maxDepth(TreeNode* root) { | ||
if (root == NULL) return 0; | ||
else { | ||
int l = maxDepth(root->left); | ||
int r = maxDepth(root->right); | ||
int m = max(l, r); | ||
return m + 1; | ||
} | ||
} | ||
}; | ||
``` | ||
## 学习感想 | ||
递归 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# 111. 二叉树的最小深度 | ||
## 题目描述 | ||
|
||
给定一个二叉树,找出其最小深度。 | ||
|
||
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 | ||
|
||
说明:叶子节点是指没有子节点的节点。 | ||
|
||
## 解题思路 | ||
|
||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int minDepth(TreeNode* root) { | ||
if (root == NULL) return 0; | ||
if (root->left == NULL && root->right == NULL) return 1; | ||
if (root->left == NULL) return minDepth(root->right) + 1; | ||
if (root->right == NULL) return minDepth(root->left) + 1; | ||
else return min(minDepth(root->left), minDepth(root->right)) + 1; | ||
} | ||
}; | ||
``` | ||
## 学习感想 | ||
写到这里我根本没有想明白为什么这个是对的。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# 222. 完全二叉树的节点个数 | ||
|
||
## 题目描述 | ||
|
||
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。 | ||
|
||
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。 | ||
|
||
## 解题思路 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int countNodes(TreeNode* root) { | ||
if (root == NULL) return 0; | ||
int leftcnt = 0; | ||
int rightcnt = 0; | ||
TreeNode* ptr = root; | ||
while (ptr ->left != NULL) { | ||
ptr = ptr->left; | ||
leftcnt ++ ; | ||
} | ||
ptr = root; | ||
while (ptr ->right != NULL) { | ||
ptr = ptr->right; | ||
leftcnt ++ ; | ||
} | ||
if (leftcnt == rightcnt) { | ||
return (2 << leftcnt) - 1; | ||
} | ||
else { | ||
return countNodes(root->left) + countNodes(root->right) + 1; | ||
} | ||
|
||
} | ||
}; | ||
``` | ||
|
||
## 学习感想 | ||
|
||
时间复杂度为Logn |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 第六章 二叉树part04 | ||
|
||
今日内容: | ||
|
||
● 110.平衡二叉树 | ||
● 257. 二叉树的所有路径 | ||
● 404.左叶子之和 | ||
|
||
详细布置 | ||
|
||
迭代法,大家可以直接过,二刷有精力的时候 再去掌握迭代法。 | ||
|
||
## 110.平衡二叉树 (优先掌握递归) | ||
|
||
再一次涉及到,什么是高度,什么是深度,可以巩固一下。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html | ||
|
||
## 257. 二叉树的所有路径 (优先掌握递归) | ||
|
||
这是大家第一次接触到回溯的过程, 我在视频里重点讲解了 本题为什么要有回溯,已经回溯的过程。 | ||
|
||
如果对回溯 似懂非懂,没关系, 可以先有个印象。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html | ||
|
||
## 404.左叶子之和 (优先掌握递归) | ||
|
||
其实本题有点文字游戏,搞清楚什么是左叶子,剩下的就是二叉树的基本操作。 | ||
|
||
题目链接/文章讲解/视频讲解:https://programmercarl.com/0404.%E5%B7%A6%E5%8F%B6%E5%AD%90%E4%B9%8B%E5%92%8C.html |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 110. 平衡二叉树 | ||
|
||
## 题目描述 | ||
|
||
给定一个二叉树,判断它是否是高度平衡的二叉树。 | ||
|
||
本题中,一棵高度平衡二叉树定义为: | ||
|
||
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1 。 | ||
|
||
## 解题思路 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int f(TreeNode* root) { | ||
if (root == NULL) return 0; | ||
int l = f(root->left); | ||
if (l == -1) return -1; | ||
int r = f(root->right); | ||
if (r == -1) return -1; | ||
if (abs(l-r)>1) return -1; | ||
return max(l,r)+1; | ||
} | ||
|
||
bool isBalanced(TreeNode* root) { | ||
return f(root) != -1 ? true : false; | ||
} | ||
}; | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# 257. 二叉树的所有路径 | ||
## 题目描述 | ||
|
||
给你一个二叉树的根节点 root ,按 任意顺序 ,返回所有从根节点到叶子节点的路径。 | ||
|
||
叶子节点 是指没有子节点的节点。 | ||
|
||
## 解题思路 | ||
|
||
```cpp | ||
class Solution { | ||
void f(TreeNode* root, string path, vector<string> &res) { | ||
path += to_string(root->val); | ||
if (!root->left && !root->right) { | ||
res.push_back(path); | ||
return; | ||
} | ||
if(root->left)f(root->left,path+"->",res); | ||
if(root->right)f(root->right,path+"->",res); | ||
} | ||
public: | ||
vector<string> binaryTreePaths(TreeNode* root) { | ||
vector<string> res; | ||
string path; | ||
if (!root) return res; | ||
f(root, path, res); | ||
return res; | ||
} | ||
}; | ||
``` | ||
## 学习感想 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# 404. 左叶子之和 | ||
|
||
## 题目描述 | ||
|
||
给定二叉树的根节点 root ,返回所有左叶子之和。 | ||
|
||
## 解题思路 | ||
|
||
```cpp | ||
class Solution { | ||
public: | ||
int sumOfLeftLeaves(TreeNode* root) { | ||
if (root == NULL) return 0; | ||
int r=0; | ||
if (root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL)r+=root->left->val; | ||
return r+sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right); | ||
} | ||
}; | ||
``` | ||
## 学习感想 |