求二叉树的深度
思路:
分别递归左右子树,深度=左右子树中大的一个+1
/*
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 leftDepth=TreeDepth(pRoot->left);
int rightDepth=TreeDepth(pRoot->right);
return leftDepth>rightDepth?leftDepth+1:rightDepth+1;
}
};
输入一颗二叉树的根节点,判断该树是不是平衡二叉树</