zoukankan      html  css  js  c++  java
  • 《剑指offer》-判断平衡二叉树

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

    考察平衡树的概念和递归的使用。平衡树是指,树中的每个节点的左右子树的高度差小于等于1。

    class Solution {
    public:
        bool IsBalanced_Solution(TreeNode* pRoot) {
    		if(pRoot == NULL){
                return true;
            }
            if(IsBalanced_Solution(pRoot->left) && IsBalanced_Solution(pRoot->right)){
                int left_depth = getDepth(pRoot->left);
                int right_depth = getDepth(pRoot->right);
                if(abs(left_depth-right_depth)<=1){
                    return true;
                }
                return false;
            }
            return false;
        }
        int getDepth(TreeNode* pRoot){
            if(pRoot == NULL){
                return 0;
            }
            return 1+max(getDepth(pRoot->left), getDepth(pRoot->right));
        }
    };
    
  • 相关阅读:
    安装wamp的方法及过程
    js原生获取className&多选一
    构造函数
    轮播图
    NaN
    ++与--运算练习
    if语句的练习
    switch语句的练习
    九九乘法表
    mac下git提交github代码
  • 原文地址:https://www.cnblogs.com/zjutzz/p/6501682.html
Copyright © 2011-2022 走看看