mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
@ -125,9 +125,10 @@ public:
|
||||
|
||||
1. 明确递归函数的参数和返回值
|
||||
|
||||
参数的话为传入的节点指针,就没有其他参数需要传递了,返回值要返回传入节点为根节点树的深度。
|
||||
参数:当前传入节点。
|
||||
返回值:以当前传入节点为根节点的树的高度。
|
||||
|
||||
那么如何标记左右子树是否差值大于1呢。
|
||||
那么如何标记左右子树是否差值大于1呢?
|
||||
|
||||
如果当前传入节点为根节点的二叉树已经不是二叉平衡树了,还返回高度的话就没有意义了。
|
||||
|
||||
@ -136,9 +137,9 @@ public:
|
||||
代码如下:
|
||||
|
||||
|
||||
```
|
||||
```CPP
|
||||
// -1 表示已经不是平衡二叉树了,否则返回值是以该节点为根节点树的高度
|
||||
int getDepth(TreeNode* node)
|
||||
int getHeight(TreeNode* node)
|
||||
```
|
||||
|
||||
2. 明确终止条件
|
||||
@ -147,7 +148,7 @@ int getDepth(TreeNode* node)
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```CPP
|
||||
if (node == NULL) {
|
||||
return 0;
|
||||
}
|
||||
@ -155,23 +156,23 @@ if (node == NULL) {
|
||||
|
||||
3. 明确单层递归的逻辑
|
||||
|
||||
如何判断当前传入节点为根节点的二叉树是否是平衡二叉树呢,当然是左子树高度和右子树高度相差。
|
||||
如何判断以当前传入节点为根节点的二叉树是否是平衡二叉树呢?当然是其左子树高度和其右子树高度的差值。
|
||||
|
||||
分别求出左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉树了。
|
||||
分别求出其左右子树的高度,然后如果差值小于等于1,则返回当前二叉树的高度,否则则返回-1,表示已经不是二叉平衡树了。
|
||||
|
||||
代码如下:
|
||||
|
||||
```CPP
|
||||
int leftDepth = depth(node->left); // 左
|
||||
if (leftDepth == -1) return -1;
|
||||
int rightDepth = depth(node->right); // 右
|
||||
if (rightDepth == -1) return -1;
|
||||
int leftHeight = getHeight(node->left); // 左
|
||||
if (leftHeight == -1) return -1;
|
||||
int rightHeight = getHeight(node->right); // 右
|
||||
if (rightHeight == -1) return -1;
|
||||
|
||||
int result;
|
||||
if (abs(leftDepth - rightDepth) > 1) { // 中
|
||||
if (abs(leftHeight - rightHeight) > 1) { // 中
|
||||
result = -1;
|
||||
} else {
|
||||
result = 1 + max(leftDepth, rightDepth); // 以当前节点为根节点的最大高度
|
||||
result = 1 + max(leftHeight, rightHeight); // 以当前节点为根节点的树的最大高度
|
||||
}
|
||||
|
||||
return result;
|
||||
@ -180,27 +181,27 @@ return result;
|
||||
代码精简之后如下:
|
||||
|
||||
```CPP
|
||||
int leftDepth = getDepth(node->left);
|
||||
if (leftDepth == -1) return -1;
|
||||
int rightDepth = getDepth(node->right);
|
||||
if (rightDepth == -1) return -1;
|
||||
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
|
||||
int leftHeight = getHeight(node->left);
|
||||
if (leftHeight == -1) return -1;
|
||||
int rightHeight = getHeight(node->right);
|
||||
if (rightHeight == -1) return -1;
|
||||
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
|
||||
```
|
||||
|
||||
此时递归的函数就已经写出来了,这个递归的函数传入节点指针,返回以该节点为根节点的二叉树的高度,如果不是二叉平衡树,则返回-1。
|
||||
|
||||
getDepth整体代码如下:
|
||||
getHeight整体代码如下:
|
||||
|
||||
```CPP
|
||||
int getDepth(TreeNode* node) {
|
||||
int getHeight(TreeNode* node) {
|
||||
if (node == NULL) {
|
||||
return 0;
|
||||
}
|
||||
int leftDepth = getDepth(node->left);
|
||||
if (leftDepth == -1) return -1;
|
||||
int rightDepth = getDepth(node->right);
|
||||
if (rightDepth == -1) return -1;
|
||||
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
|
||||
int leftHeight = getHeight(node->left);
|
||||
if (leftHeight == -1) return -1;
|
||||
int rightHeight = getHeight(node->right);
|
||||
if (rightHeight == -1) return -1;
|
||||
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
|
||||
}
|
||||
```
|
||||
|
||||
@ -210,18 +211,18 @@ int getDepth(TreeNode* node) {
|
||||
class Solution {
|
||||
public:
|
||||
// 返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
|
||||
int getDepth(TreeNode* node) {
|
||||
int getHeight(TreeNode* node) {
|
||||
if (node == NULL) {
|
||||
return 0;
|
||||
}
|
||||
int leftDepth = getDepth(node->left);
|
||||
if (leftDepth == -1) return -1; // 说明左子树已经不是二叉平衡树
|
||||
int rightDepth = getDepth(node->right);
|
||||
if (rightDepth == -1) return -1; // 说明右子树已经不是二叉平衡树
|
||||
return abs(leftDepth - rightDepth) > 1 ? -1 : 1 + max(leftDepth, rightDepth);
|
||||
int leftHeight = getHeight(node->left);
|
||||
if (leftHeight == -1) return -1;
|
||||
int rightHeight = getHeight(node->right);
|
||||
if (rightHeight == -1) return -1;
|
||||
return abs(leftHeight - rightHeight) > 1 ? -1 : 1 + max(leftHeight, rightHeight);
|
||||
}
|
||||
bool isBalanced(TreeNode* root) {
|
||||
return getDepth(root) == -1 ? false : true;
|
||||
return getHeight(root) == -1 ? false : true;
|
||||
}
|
||||
};
|
||||
```
|
||||
@ -498,20 +499,35 @@ class Solution {
|
||||
## Python
|
||||
|
||||
递归法:
|
||||
```python
|
||||
```python3
|
||||
# Definition for a binary tree node.
|
||||
# class TreeNode:
|
||||
# def __init__(self, val=0, left=None, right=None):
|
||||
# self.val = val
|
||||
# self.left = left
|
||||
# self.right = right
|
||||
class Solution:
|
||||
def isBalanced(self, root: TreeNode) -> bool:
|
||||
return True if self.getDepth(root) != -1 else False
|
||||
if self.get_height(root) != -1:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
#返回以该节点为根节点的二叉树的高度,如果不是二叉搜索树了则返回-1
|
||||
def getDepth(self, node):
|
||||
if not node:
|
||||
def get_height(self, root: TreeNode) -> int:
|
||||
# Base Case
|
||||
if not root:
|
||||
return 0
|
||||
leftDepth = self.getDepth(node.left)
|
||||
if leftDepth == -1: return -1 #说明左子树已经不是二叉平衡树
|
||||
rightDepth = self.getDepth(node.right)
|
||||
if rightDepth == -1: return -1 #说明右子树已经不是二叉平衡树
|
||||
return -1 if abs(leftDepth - rightDepth)>1 else 1 + max(leftDepth, rightDepth)
|
||||
# 左
|
||||
if (left_height := self.get_height(root.left)) == -1:
|
||||
return -1
|
||||
# 右
|
||||
if (right_height := self.get_height(root.right)) == -1:
|
||||
return -1
|
||||
# 中
|
||||
if abs(left_height - right_height) > 1:
|
||||
return -1
|
||||
else:
|
||||
return 1 + max(left_height, right_height)
|
||||
```
|
||||
|
||||
迭代法:
|
||||
|
Reference in New Issue
Block a user