Fork me on GitHub

【剑指offer】平衡二叉树

题目描述

输入一棵二叉树,判断该二叉树是否是平衡二叉树。

题目地址

解决方案

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL) return true;
if(abs(getDepth(pRoot->left) - getDepth(pRoot->right)) > 1)
return false;
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}

int getDepth(TreeNode* pRoot){
if(pRoot==NULL)
return 0;
return max(getDepth(pRoot->left),getDepth(pRoot->right))+1;
}
};

本文标题:【剑指offer】平衡二叉树

文章作者:LiuXiaoKun

发布时间:2019年03月30日 - 23:03

最后更新:2019年03月30日 - 23:03

原始链接:https://LiuZiQiao.github.io/2019/03/30/【剑指offer】平衡二叉树/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

0%