求树的深度
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot == NULL) return 0;
int left = TreeDepth(pRoot->left);
int right = TreeDepth(pRoot->right);
return (left>right) ? left+1: right+1;
}
};
判断是否是平衡树
class Solution {
public:
int TreeDepth(TreeNode* pRoot){
if(pRoot == NULL) return 0;
int left = TreeDepth(pRoot->left);
int right = TreeDepth(pRoot->right);
return (left>right)? left+1: right+1;
}
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot == NULL) return true;
int leftNum = TreeDepth(pRoot->left);
int rightNum = TreeDepth(pRoot->right);
if(abs(leftNum-rightNum)>1)
return false;
return IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right);
}
};