Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 2 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions problems/0098.验证二叉搜索树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,15 @@ public:

写出了类似这样的代码:

```
```CPP
if (root->val > root->left->val && root->val < root->right->val) {
return true;
} else {
return false;
}
```

**我们要比较的是 左子树所有节点小于中间节点,右子树所有节点大于中间节点。**所以以上代码的判断逻辑是错误的。
**我们要比较的是 左子树所有节点小于中间节点,右子树所有节点大于中间节点**。所以以上代码的判断逻辑是错误的。

例如: [10,5,15,null,null,6,20] 这个case:

Expand Down Expand Up @@ -125,7 +125,7 @@ if (root->val > root->left->val && root->val < root->right->val) {

代码如下:

```
```CPP
long long maxVal = LONG_MIN; // 因为后台测试数据中有int最小值
bool isValidBST(TreeNode* root)
```
Expand All @@ -138,7 +138,7 @@ bool isValidBST(TreeNode* root)

代码如下:

```
```CPP
if (root == NULL) return true;
```

Expand All @@ -148,7 +148,7 @@ if (root == NULL) return true;

代码如下:

```
```CPP
bool left = isValidBST(root->left); // 左

// 中序遍历,验证遍历的元素是不是从小到大
Expand Down
15 changes: 10 additions & 5 deletions problems/0104.二叉树的最大深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>


看完本篇可以一起做了如下两道题目:

* 104.二叉树的最大深度
* 559.n叉树的最大深度

# 104.二叉树的最大深度

[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
Expand All @@ -27,6 +22,16 @@

返回它的最大深度 3 。

# 思路

看完本篇可以一起做了如下两道题目:

* 104.二叉树的最大深度
* 559.n叉树的最大深度

《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。


## 递归法

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
Expand Down
28 changes: 15 additions & 13 deletions problems/0110.平衡二叉树.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@

返回 false 。

# 题外话

**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。

## 题外话

咋眼一看这道题目和[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)很像,其实有很大区别。

Expand Down Expand Up @@ -113,9 +115,9 @@ public:
};
```

# 本题思路
## 本题思路

## 递归
### 递归

此时大家应该明白了既然要求比较高度,必然是要后序遍历。

Expand Down Expand Up @@ -225,7 +227,7 @@ public:
};
```

## 迭代
### 迭代

在[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)中我们可以使用层序遍历来求深度,但是就不能直接用层序遍历来求高度了,这就体现出求高度和求深度的不同。

Expand Down Expand Up @@ -342,7 +344,7 @@ public:

因为对于回溯算法已经是非常复杂的递归了,如果在用迭代的话,就是自己给自己找麻烦,效率也并不一定高。

# 总结
## 总结

通过本题可以了解求二叉树深度 和 二叉树高度的差异,求深度适合用前序遍历,而求高度适合用后序遍历。

Expand All @@ -351,9 +353,9 @@ public:
但是递归方式是一定要掌握的!


# 其他语言版本
## 其他语言版本

## Java
### Java

```Java
class Solution {
Expand Down Expand Up @@ -494,7 +496,7 @@ class Solution {
}
```

## Python
### Python

递归法:
```python
Expand Down Expand Up @@ -554,7 +556,7 @@ class Solution:
```


## Go
### Go
```Go
func isBalanced(root *TreeNode) bool {
if root==nil{
Expand Down Expand Up @@ -590,7 +592,7 @@ func abs(a int)int{
}
```

## JavaScript
### JavaScript
递归法:
```javascript
var isBalanced = function(root) {
Expand Down Expand Up @@ -658,7 +660,7 @@ var isBalanced = function (root) {
};
```

## TypeScript
### TypeScript

```typescript
// 递归法
Expand All @@ -676,7 +678,7 @@ function isBalanced(root: TreeNode | null): boolean {
};
```

## C
### C

递归法:
```c
Expand Down Expand Up @@ -780,7 +782,7 @@ bool isBalanced(struct TreeNode* root){
}
```

## Swift:
### Swift:

>递归
```swift
Expand Down
3 changes: 3 additions & 0 deletions problems/0111.二叉树的最小深度.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@

# 思路

《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。


看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。

直觉上好像和求最大深度差不多,其实还是差不少的。
Expand Down
24 changes: 12 additions & 12 deletions problems/0112.路径总和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ return false;
整体代码如下:

```cpp
class solution {
class Solution {
private:
bool traversal(treenode* cur, int count) {
bool traversal(TreeNode* cur, int count) {
if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回

Expand All @@ -143,8 +143,8 @@ private:
}

public:
bool haspathsum(treenode* root, int sum) {
if (root == null) return false;
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
return traversal(root, sum - root->val);
}
};
Expand All @@ -155,7 +155,7 @@ public:
```cpp
class solution {
public:
bool haspathsum(treenode* root, int sum) {
bool hasPathSum(TreeNode* root, int sum) {
if (root == null) return false;
if (!root->left && !root->right && sum == root->val) {
return true;
Expand All @@ -176,7 +176,7 @@ public:

c++就我们用pair结构来存放这个栈里的元素。

定义为:`pair<treenode*, int>` pair<节点指针,路径数值>
定义为:`pair<TreeNode*, int>` pair<节点指针,路径数值>

这个为栈里的一个元素。

Expand All @@ -186,25 +186,25 @@ c++就我们用pair结构来存放这个栈里的元素。
class solution {

public:
bool haspathsum(treenode* root, int sum) {
bool haspathsum(TreeNode* root, int sum) {
if (root == null) return false;
// 此时栈里要放的是pair<节点指针,路径数值>
stack<pair<treenode*, int>> st;
st.push(pair<treenode*, int>(root, root->val));
stack<pair<TreeNode*, int>> st;
st.push(pair<TreeNode*, int>(root, root->val));
while (!st.empty()) {
pair<treenode*, int> node = st.top();
pair<TreeNode*, int> node = st.top();
st.pop();
// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回true
if (!node.first->left && !node.first->right && sum == node.second) return true;

// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if (node.first->right) {
st.push(pair<treenode*, int>(node.first->right, node.second + node.first->right->val));
st.push(pair<TreeNode*, int>(node.first->right, node.second + node.first->right->val));
}

// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来
if (node.first->left) {
st.push(pair<treenode*, int>(node.first->left, node.second + node.first->left->val));
st.push(pair<TreeNode*, int>(node.first->left, node.second + node.first->left->val));
}
}
return false;
Expand Down
2 changes: 2 additions & 0 deletions problems/0222.完全二叉树的节点个数.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

# 思路

《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。

本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。

## 普通二叉树
Expand Down
2 changes: 2 additions & 0 deletions problems/0257.二叉树的所有路径.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

# 思路

**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。

这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。

在这道题目中将第一次涉及到回溯,因为我们要把路径记录下来,需要回溯来回退一一个路径在进入另一个路径。
Expand Down
3 changes: 3 additions & 0 deletions problems/0404.左叶子之和.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

# 思路

**《代码随想录》算法视频公开课:[二叉树的题目中,总有一些规则让你找不到北 | LeetCode:404.左叶子之和](https://www.bilibili.com/video/BV1GY4y1K7z8),相信结合视频在看本篇题解,更有助于大家对本题的理解**。


**首先要注意是判断左叶子,不是二叉树左侧节点,所以不要上来想着层序遍历。**

因为题目中其实没有说清楚左叶子究竟是什么节点,那么我来给出左叶子的明确定义:**节点A的左孩子不为空,且左孩子的左右孩子都为空(说明是叶子节点),那么A节点的左孩子为左叶子节点**
Expand Down
8 changes: 4 additions & 4 deletions problems/0501.二叉搜索树中的众数.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void searchBST(TreeNode* cur, unordered_map<int, int>& map) { // 前序遍历

代码如下:

```
```CPP
bool static cmp (const pair<int, int>& a, const pair<int, int>& b) {
return a.second > b.second; // 按照频率从大到小排序
}
Expand Down Expand Up @@ -169,7 +169,7 @@ void searchBST(TreeNode* cur) {

代码如下:

```
```CPP
if (pre == NULL) { // 第一个节点
count = 1; // 频率为1
} else if (pre->val == cur->val) { // 与前一个节点数值相同
Expand All @@ -194,7 +194,7 @@ pre = cur; // 更新上一个节点

如果 频率count 等于 maxCount(最大频率),当然要把这个元素加入到结果集中(以下代码为result数组),代码如下:

```
```CPP
if (count == maxCount) { // 如果和最大值相同,放进result中
result.push_back(cur->val);
}
Expand All @@ -206,7 +206,7 @@ if (count == maxCount) { // 如果和最大值相同,放进result中

频率count 大于 maxCount的时候,不仅要更新maxCount,而且要清空结果集(以下代码为result数组),因为结果集之前的元素都失效了。

```
```CPP
if (count > maxCount) { // 如果计数大于最大值
maxCount = count; // 更新最大频率
result.clear(); // 很关键的一步,不要忘记清空result,之前result里的元素都失效了
Expand Down
Loading

AltStyle によって変換されたページ (->オリジナル) /