Skip to content

Commit 7537374

Browse files
committed
feat: 更新平衡二叉树
1 parent 254cb10 commit 7537374

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

leetcode/binary_tree/balanced_binary_tree.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ struct TreeNode
3838
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
3939
};
4040

41+
/**
42+
* 方法一:自顶向下的递归
43+
*
44+
* 题解:
45+
*
46+
* 来源:https://leetcode-cn.com/problems/balanced-binary-tree/solution/ping-heng-er-cha-shu-by-leetcode-solution/
47+
*
48+
* 复杂度:
49+
* 时间复杂度:O(n^2),其中 n 是二叉树中的节点个数。
50+
* 空间复杂度:O(n),其中 n 是二叉树中的节点个数。空间复杂度主要取决于递归调用的层数,递归调用的层数不会超过 n
51+
*
52+
*/
53+
class Solution
54+
{
55+
private:
56+
/* data */
57+
public:
58+
bool isBalanced(TreeNode *root)
59+
{
60+
}
61+
62+
int height(TreeNode *root)
63+
{
64+
65+
return 0;
66+
}
67+
};
68+
4169
/**
4270
* 方法二:自底向上的递归
4371
*
@@ -48,8 +76,13 @@ struct TreeNode
4876
* 自底向上递归类似于后序遍历,对于当前遍历到的节点,先递归地判断其左右子树是否平衡,再判断以当前节点为根的子树是否平衡。如果一棵子树是
4977
* 平衡的,则返回其高度(高度一定是非负整数),否则返回 -1.如果存在一棵子树不平衡,则整个二叉树一定不平衡。
5078
*
79+
* 复杂度:
80+
* 时间复杂度:O(n),其中 n 是二叉树中的节点个数。使用自底向上的递归,每个节点的计算高度和判断是否平衡都只需要处理一次,
81+
* 最坏情况下需要遍历二叉树中的所有节点,因此时间复杂度是 O(n)。
82+
* 空间复杂度:O(n),其中 n 是二叉树中的节点个数。空间复杂度主要取决于递归调用的层数,递归调用的层数不会超过 n
83+
*
5184
*/
52-
class Solution
85+
class Solution1
5386
{
5487
public:
5588
bool isBalanced(TreeNode *root)

0 commit comments

Comments
 (0)