更新图床

This commit is contained in:
programmercarl
2023-03-10 14:02:32 +08:00
parent 2a9b627a90
commit 17cb4b45c7
134 changed files with 1169 additions and 829 deletions

View File

@@ -1,3 +1,4 @@
<p align="center">
<a href="https://programmercarl.com/other/xunlianying.html" target="_blank">
<img src="../pics/训练营.png" width="1000"/>
@@ -5,6 +6,7 @@
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
> 求高度还是求深度,你搞懂了不?
# 110.平衡二叉树
@@ -13,13 +15,13 @@
给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
本题中,一棵高度平衡二叉树定义为:一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树 [3,9,20,null,null,15,7]
![110.平衡二叉树](https://img-blog.csdnimg.cn/2021020315542230.png)
![110.平衡二叉树](https://code-thinking-1253855093.file.myqcloud.com/pics/2021020315542230.png)
返回 true 。
@@ -27,7 +29,7 @@
给定二叉树 [1,2,2,3,3,null,null,4,4]
![110.平衡二叉树1](https://img-blog.csdnimg.cn/20210203155447919.png)
![110.平衡二叉树1](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155447919.png)
返回 false 。
@@ -45,7 +47,7 @@
但leetcode中强调的深度和高度很明显是按照节点来计算的如图
![110.平衡二叉树2](https://img-blog.csdnimg.cn/20210203155515650.png)
![110.平衡二叉树2](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203155515650.png)
关于根节点的深度究竟是1 还是 0不同的地方有不一样的标准leetcode的题目中都是以节点为一度即根节点深度是1。但维基百科上定义用边为一度即根节点的深度是0我们暂时以leetcode为准毕竟要在这上面刷题
@@ -125,7 +127,7 @@ public:
1. 明确递归函数的参数和返回值
参数:当前传入节点。
参数:当前传入节点。
返回值:以当前传入节点为根节点的树的高度。
那么如何标记左右子树是否差值大于1呢
@@ -496,9 +498,10 @@ class Solution {
}
```
### Python
### Python
递归法:
```python
# Definition for a binary tree node.
# class TreeNode:
@@ -512,7 +515,7 @@ class Solution:
return True
else:
return False
def get_height(self, root: TreeNode) -> int:
# Base Case
if not root:
@@ -531,6 +534,7 @@ class Solution:
```
迭代法:
```python
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
@@ -557,9 +561,10 @@ class Solution:
### Go
```Go
func isBalanced(root *TreeNode) bool {
h := getHeight(root)
h := getHeight(root)
if h == -1 {
return false
}
@@ -588,7 +593,9 @@ func max(a, b int) int {
```
### JavaScript
递归法:
递归法:
```javascript
var isBalanced = function(root) {
//还是用递归三部曲 + 后序遍历 左右中 当前左子树右子树高度相差大于1就返回-1
@@ -614,6 +621,7 @@ var isBalanced = function(root) {
```
迭代法:
```javascript
// 获取当前节点的高度
var getHeight = function (curNode) {
@@ -644,7 +652,7 @@ var isBalanced = function (root) {
let queue = [root];
while (queue.length) {
let node = queue[queue.length - 1]; // 取出栈顶
queue.pop();
queue.pop();
if (Math.abs(getHeight(node.left) - getHeight(node.right)) > 1) {
return false;
}
@@ -676,6 +684,7 @@ function isBalanced(root: TreeNode | null): boolean {
### C
递归法:
```c
int getDepth(struct TreeNode* node) {
//如果结点不存在返回0
@@ -706,6 +715,7 @@ bool isBalanced(struct TreeNode* root) {
```
迭代法:
```c
//计算结点深度
int getDepth(struct TreeNode* node) {
@@ -717,7 +727,7 @@ int getDepth(struct TreeNode* node) {
stack[stackTop++] = node;
int result = 0;
int depth = 0;
//当栈中有元素时,进行迭代遍历
while(stackTop) {
//取出栈顶元素
@@ -741,7 +751,7 @@ int getDepth(struct TreeNode* node) {
tempNode = stack[--stackTop];
depth--;
}
}
}
return result;
}
@@ -750,11 +760,11 @@ bool isBalanced(struct TreeNode* root){
//开辟栈空间
struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 10000);
int stackTop = 0;
//若根节点不存在返回True
if(!root)
return 1;
//将根节点入栈
stack[stackTop++] = root;
//当栈中有元素时,进行遍历
@@ -764,7 +774,7 @@ bool isBalanced(struct TreeNode* root){
//计算左右子树的深度
int diff = getDepth(node->right) - getDepth(node->left);
//若深度的绝对值大于1返回False
if(diff > 1 || diff < -1)
if(diff > 1 || diff < -1)
return 0;
//如果栈顶结点有左右结点,将左右结点入栈
if(node->left)
@@ -780,6 +790,7 @@ bool isBalanced(struct TreeNode* root){
### Swift:
>递归
```swift
func isBalanced(_ root: TreeNode?) -> Bool {
// -1 已经不是平衡二叉树