balanced-binary-tree
来源:互联网 发布:mysql导入sql文件命令 编辑:程序博客网 时间:2024/06/17 21:44
题目:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
程序:
class Solution {public: int depth(TreeNode *root){ if (root == NULL) return 0; int leftDepth = depth(root->left); int rightDepth = depth(root->right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } bool isBalanced(TreeNode *root) { if (root == NULL) return true; int diffDepth = depth(root->left) - depth(root->right); if (diffDepth >= -1 && diffDepth <= 1){ return (isBalanced(root->left) && isBalanced(root->right)); } else{ return false; } }};
阅读全文
0 0
- Balanced binary tree 扩展
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- Balanced Binary Tree
- Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- SpringBoot通过jar包方式centos后台部署,如何做到退出控制台,链接访问有效
- MySQL查询TOP(n)
- 服务号和订阅号的区别-微信公众号使用教程5
- Elasticsearch的路由(Routing)特性
- 两个Tomcat的安装配置
- balanced-binary-tree
- Keras里的模块介绍
- javaweb 常见问题-第2篇- chrom google 浏览器 调试时js无法刷新的解决方法
- BZOJ2728
- java多线程_死锁问题_用最简单的代码重现死锁问题
- 面试常见问题--数据库优化 百万数据怎么优化
- dede用htm2text函数来去掉html格式
- 二分查找(折半查找)
- create-react-app按需加载以及部署