zoukankan      html  css  js  c++  java
  • 平衡二叉树

    题目描述

    输入一棵二叉树,判断该二叉树是否是平衡二叉树。
     
     1 class Solution {
     2 public:
     3     int TreeDepth(TreeNode* pRoot)
     4     {
     5         if(pRoot == NULL)
     6             return 0;
     7         int cnt1 = TreeDepth(pRoot->left);
     8         int cnt2 = TreeDepth(pRoot->right);
     9         return cnt1 > cnt2 ? cnt1 +1 : cnt2+1;
    10     }
    11      bool IsBalanced_Solution(TreeNode* pRoot) {
    12          if (pRoot == NULL)
    13          {
    14             return 1;
    15          }
    16          int cnt1 = TreeDepth(pRoot->left);
    17          int cnt2 = TreeDepth(pRoot->right);
    18          if (cnt1 - cnt2 > 1 || cnt1 - cnt2 < -1 || !(IsBalanced_Solution(pRoot->left)) || !(IsBalanced_Solution(pRoot->right)) )
    19          {
    20             return 0;
    21          }
    22          return 1;
    23     }
    24 };
  • 相关阅读:
    SQL Server
    SQL Server
    SQL Server
    SQL Server
    SQL Server
    SQL Server
    SQL Server
    ssh保持连接不断开
    如何查看linux是否打开虚拟化
    dd命令详解
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5160183.html
Copyright © 2011-2022 走看看