本文主要是介绍剑指offer(C++)--平衡二叉树,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题目
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
class Solution {
public:bool IsBalanced_Solution(TreeNode* pRoot) {if(pRoot==NULL)return true;int left_depth = getdepth(pRoot->left);int right_depth = getdepth(pRoot->right);if(left_depth>right_depth+1 || left_depth+1<right_depth)return false;elsereturn IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);}int getdepth(TreeNode* pRoot){if(pRoot==NULL)return 0;return max(1+getdepth(pRoot->left),1+getdepth(pRoot->right));}
};
这篇关于剑指offer(C++)--平衡二叉树的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!