原题链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof/

1、后序遍历 + 剪枝

bool isBalanced(TreeNode* root) {return dfs(root) != -1;
}int dfs(TreeNode* root) {if (root == NULL) return 0;int depthLeft = dfs(root->left);if (depthLeft == -1) return -1;int depthRight = dfs(root->right);if (depthRight == -1) return -1;return abs(depthLeft - depthRight) < 2 ? max(depthLeft, depthRight) + 1 : -1 ;
}

2、先序遍历 + 判断深度

bool isBalanced(TreeNode* root) {if(root == NULL) return true;return abs(depth(root->right) - depth(root->left)) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}int depth(TreeNode* root) {if (root == NULL) return 0;return max(depth(root->left), depth(root->right)) + 1;
}

leetcode算法题--平衡二叉树相关推荐

  1. LeetCode算法题整理(200题左右)

    目录 前言 一.树(17) 1.1.后序遍历 1.2.层次遍历 1.3.中序 1.4.前序 二.回溯(20) 2.1.普通回溯 2.2.线性回溯:组合.排列.子集.分割 2.3.矩阵回溯 三.二分查找 ...

  2. LeetCode算法题-Nth Digit(Java实现)

    这是悦乐书的第215次更新,第228篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第83题(顺位题号是400).找到无限整数序列的第n个数字1,2,3,4,5,6,7,8 ...

  3. LeetCode算法题-Reverse Linked List(Java实现)

    这是悦乐书的第192次更新,第195篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第51题(顺位题号是206).反转单链表.例如: 输入:1-> 2-> 3- ...

  4. LeetCode算法题-Convert a Number to Hexadecimal(Java实现)

    这是悦乐书的第219次更新,第231篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第86题(顺位题号是405).给定一个整数,写一个算法将其转换为十六进制.对于负整数,使 ...

  5. leetcode算法题--零钱兑换

    原题链接:https://leetcode-cn.com/problems/coin-change/ 相关题目:leetcode算法题–完全平方数★ 动态规划 dp[i] i从0到amount,dp[ ...

  6. leetcode算法题-- 买卖股票的最佳时机

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 这类股票题目请见leetcode算法题–最佳买卖股票时机含 ...

  7. leetcode算法题--买卖股票的最佳时机 II

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/ 这类股票题目请见leetcode算法题–最佳买卖股票 ...

  8. leetcode算法题--买卖股票的最佳时机含手续费

    原题链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/ 这类股票题目请见 ...

  9. leetcode算法题--完全平方数★

    原题链接:https://leetcode-cn.com/problems/perfect-squares/ 相关题目:leetcode算法题–零钱兑换 1.回溯法 就是暴力法,套路就是递归,但是有很 ...

最新文章

  1. CALayer 了解与使用
  2. setTimeOut函数和setInterval函数
  3. GridView添加自动编号列
  4. c语言求5个数最小公倍数,C语言,求从键盘输入的五个自然数的最小公倍数
  5. 线框图(demo草图)制作的总结
  6. 【经验总结】VS2010下建立MFC程序
  7. QT的QCameraInfo类的使用
  8. 合成孔径成像算法与实现_MIMO OFDM宽幅SAR成像仿真与图像融合技术
  9. jaxp与dom4j遍历xml树
  10. Spring 的 ApplicationEvent and ApplicationListener
  11. classes是什么意思怎么读_classes怎么读用法大全_classes是什么意思
  12. 深入浅出:Java多线程编程实战(一)
  13. Ansible 运维自动化 ( 配置管理工具 )
  14. 计算机中常见的循环,电脑系统提示数据错误循环冗余检查的解决方法教程[多图]...
  15. Hdu2104(主席树)
  16. marked转换html失败,解析markdown之marked
  17. 2015年上半年数据库系统工程师考试上午真题
  18. 【新人求助】苹果允许开发者制作类似按键精灵的app吗?
  19. 成都比较好的建筑设计公司有哪些?
  20. coco数据集进行裁剪、数据增强过程中的ground_truth bbox的设定以及变化

热门文章

  1. python难嘛-学python难吗
  2. python语言自学-零基础小白学习Python编程语言的正确姿势(纯干货)
  3. 北京python培训班价格-Python培训班多少钱?
  4. 用python画花瓣-Python——画一棵漂亮的樱花树
  5. python怎样画立体图形-用python来画出高光谱遥感影像的3D立体图
  6. pythonurllib模块-python模块之urllib
  7. python哪本好-最好的Python入门教材是哪本?
  8. UVa10382 - Watering Grass(贪心算法)
  9. LeetCode 495. Teemo Attacking
  10. 干货!谷歌推荐的技术能力提升指南