输入一棵二叉树,判断该二叉树是否是平衡二叉树。
在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
输入
{1,2,3,4,5,6,7}
返回值
true
最直接的做法,遍历每个结点,借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。
int maxDepth(TreeNode* node) {
if (node == nullptr) return 0;
return 1 + max(maxDepth(node->left), maxDepth(node->right));
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if (pRoot == nullptr) return true;//这里是返回true 而不再是false
return abs(maxDepth(pRoot->left) - maxDepth(pRoot->right)) <= 1 &&
IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}return 后面不需要加两个&&来递归他左子树和右子树. 这样想, 有一个函数得到了他的深度, 那么只要根的左子树和右子树深度不超过1就可以了. 后面判断的没有什么必要
上面这种做法有很明显的问题,在判断上层结点的时候,会多次重复遍历下层结点,增加了不必要的开销。如果改为从下往上遍历,如果子树是平衡二叉树,则返回子树的高度;如果发现子树不是平衡二叉树,则直接停止遍历,这样至多只对每个结点访问一次。
int getDepth(TreeNode* node) {
if (node == nullptr) return 0;
int leftDept = getDepth(node->left);
if (leftDept == -1) return -1;
int rightDept = getDepth(node->right);
if (rightDept == -1) return -1;
if (abs(leftDept - rightDept) > 1)
return -1;
else
return 1 + max(leftDept,rightDept);
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if (pRoot == nullptr) return true;//这里是返回true 而不再是false
return getDepth(pRoot)!=-1;
}这种做法有很明显的问题,在判断上层结点的时候,会多次重复遍历下层结点,增加了不必要的开销。如果改为从下往上遍历,如果子树是平衡二叉树,则返回子树的高度;如果发现子树不是平衡二叉树,则直接停止遍历,这样至多只对每个结点访问一次。
所谓平衡二叉树就是他的左孩子和右孩子的深度之差不能超过1
int getDepth(TreeNode * node){
if(node == nullptr) return 0;
int left = getDepth(node->left),right = getDepth(node->right);
return 1 + max(left,right);
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == nullptr) return true;//这里返回的是true,为空的话就应该是
return abs(getDepth(pRoot->left) - getDepth(pRoot->right))<=1;
}int getDepth(TreeNode * node){
if(node == nullptr) return 0;
int left = getDepth(node->left);
if(left == -1) return -1;
int right = getDepth(node->right);
if(right == -1) return -1;
if(abs(left - right) > 1) return -1;
else
return 1 + max(left,right);
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == nullptr) return true;
return getDepth(pRoot) != -1;
}