平衡二叉树的判断,DFS,

一直没想到怎么同时返回结点的长度和状态,把高度放到参数里面比较简便!

 1 class Solution {
 2 public:
 3     bool isBalanced(TreeNode *root) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int height = 0;
 7         return getHeight(root, height);
 8
 9     }
10     bool getHeight(TreeNode *root, int &height) {
11         if (!root) {
12             return true;
13         }
14         int leftheight = 0;
15         int rightheight = 0;
16         if (getHeight(root->left, leftheight) && getHeight(root->right, rightheight)) {
17             height = max(leftheight, rightheight) + 1;
18             if (abs(leftheight - rightheight) <= 1) {
19                 return true;
20             }
21         }
22         return false;
23     }
24 };

不过其实不需要用两个变量来表示状态,可以直接用一个int返回值来表示,

如果是false的则直接返回-1,否则返回高度:

 1 class Solution {
 2 public:
 3     bool isBalanced(TreeNode *root) {
 4         // Start typing your C/C++ solution below
 5         // DO NOT write int main() function
 6         int ret = getHeight(root);
 7         if (ret == -1) {
 8             return false;
 9         }
10         return true;
11     }
12     int getHeight(TreeNode *root) {
13         if (root == NULL) {
14             return 0;
15         }
16         int left = getHeight(root->left);
17         int right = getHeight(root->right);
18         if (left == -1 || right == - 1) {
19             return -1;
20         }
21         if (abs(left - right) > 1) {
22             return -1;
23         }
24         return (max(left, right) + 1);
25     }
26 };

转载于:https://www.cnblogs.com/chasuner/p/balanced.html

LeetCode-Balanced Binary Tree相关推荐

  1. leetcode - Balanced Binary Tree

    题目:Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a ...

  2. LeetCode Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  3. leetcode -- Balanced Binary Tree TODO

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  4. Leetcode::Balanced Binary Tree

    后序遍历,每个节点只遍历一次. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode ...

  5. Leetcode: Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary t ...

  6. LeetCode:Balanced Binary Tree

    题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...

  7. [LeetCode]Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  8. LeetCode 110 Balanced Binary Tree 平衡二叉树

    LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...

  9. LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15

    110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...

  10. LeetCode 110. Balanced Binary Tree

    LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...

最新文章

  1. Google Colab——用谷歌免费GPU跑你的深度学习代码
  2. php 清空cache,到底如何清除 cache? 我真的快瘋了
  3. vue 加载时掉用mounted_Vue实例中生命周期created和mounted的区别详解
  4. pandas数据结构:Series/DataFrame;python函数:range/arange
  5. VTK:图像亮度用法实战
  6. MusicXML 3.0 (4) - 谱号
  7. [渝粤教育] 中国地质大学 信息资源管理 复习题 (2)
  8. Mac下使用Fiddler
  9. 【java】画图和监听事件的应用
  10. mysql在linux下的完整安装
  11. Kotlin基础学习第3章—内置类型
  12. 苹果电脑修改hosts文件方法介绍!mac的hosts文件位置在哪?
  13. 人力资源经理的选择(转载)
  14. python中颜色表_python 颜色表
  15. 移动网络安装测试软件,adsl网速测试(中国移动宽带专用测速软件)
  16. EasyCamera中海康摄像头语音对讲和云台控制转发实现
  17. html标签的记忆巧法,小学记忆单词的方法
  18. 利用pyhton爬虫(案例3)--X房网的小房子们
  19. 单招计算机网络考什么,单招考什么内容 单招一般考什么科目
  20. vue 实现前端excel导出表格携带token的两种方法

热门文章

  1. Redis学习笔记(八)——持久化
  2. hdu 4336 Card Collector
  3. BZOJ 1412 狼和羊的故事
  4. SharePoint 2010、2013多个域之间互信(Domain Trust)的设计与实施
  5. 推荐7款超棒的单页面网站设计模板。关键是!免费!!
  6. 微软的公开的DLL库
  7. PHP获取 当前页面名称、主机名、URL完整地址、URL参数、获取IP
  8. 编译器对私有字段初始化的优化
  9. javascript 自动填写表单
  10. Visual Studio 2010 新功能总结(一)