[LeetCode]110. Balanced Binary Tree
来源:互联网 发布:linux安全策略 编辑:程序博客网 时间:2024/06/09 23:32
Description:
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.
———————————————————————————————————————————————————
Solution:
题意:判断一个给定二叉树是否高度平衡(左右子树高度差不超过1).
思路:递归。
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isBalanced(TreeNode* root) { return getHeight(root) != -1; } int getHeight(TreeNode* r) { if (r == NULL) return 0; int leftHeight = getHeight(r->left); if (leftHeight == -1) return -1; int rightHeight = getHeight(r->right); if (rightHeight == -1) return -1; if (abs(leftHeight - rightHeight) > 1) return -1; return 1 + max(leftHeight, rightHeight); }};
阅读全文
0 0
- LeetCode 110. Balanced Binary Tree
- [LeetCode]110.Balanced Binary Tree
- 【LeetCode】110.Balanced Binary Tree
- [Leetcode] 110. Balanced Binary Tree
- LeetCode --- 110. Balanced Binary Tree
- [leetcode] 110.Balanced Binary Tree
- [leetcode]110. Balanced Binary Tree
- LeetCode:110. Balanced Binary Tree
- 110. Balanced Binary Tree LeetCode
- [LeetCode]110. Balanced Binary Tree
- Leetcode 110. Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree
- LeetCode *** 110. Balanced Binary Tree
- leetcode 110. Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree
- #leetcode#110. Balanced Binary Tree
- leetcode-110. Balanced Binary Tree
- Spring+Struts2+hibernate实现动态切换数据源
- 对平台型产品的四问-1
- 《A Lightened CNN for Deep Face Representation》论文解读 本文来自中科院,原文地址为: https://arxiv.org/abs/1511.02683
- 创建dataframe并插入一行时报错:ValueError: Shape of passed values is (1, X), indices imply (X, X)
- JAVA的图形用户界面布局GUI入门(上)
- [LeetCode]110. Balanced Binary Tree
- ubuntu 下MySQL出现乱码的解决方法以及如何设置编码方式
- 根据IP获取所在的国家城市
- Java-抽象类(abstract)
- 上海-新的开始
- Netty自娱自乐之类Dubbo RPC 框架设计构想 【上篇】
- Python:成员运算符in 、not in
- Logistic回归原理及公式推导
- 古文觀止卷九_種樹郭橐駝傳_柳宗元