[Leetcode] Balanced Binary Tree
来源:互联网 发布:怎样用软件阅读趣头条 编辑:程序博客网 时间:2024/05/01 06:43
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.c
class Solution {public: bool isBalanced(TreeNode* root) { int depth = 0; return isBalancedHelper(root, &depth); } bool isBalancedHelper(TreeNode* root, int* depth){ if(root == nullptr){ *depth = 0; return true; } else { int depth1 = 0; int depth2 = 0; bool subtree_result = isBalancedHelper(root->left, &depth1) && isBalancedHelper(root->right, &depth2); *depth = max(depth1, depth2) + 1; return abs(depth1 - depth2) <= 1 && subtree_result; } }};
递归求是否为balanced的同时求高度
0 0
- 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
- 【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
- LeetCode - Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- LeetCode | Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- ocp-v13-078
- WebSockets
- linux shell 自定义函数
- 001-自定义视图-圆角TextView、ImageView、Button
- Substring with Concatenation of All Words
- [Leetcode] Balanced Binary Tree
- 2015 ACM/ICPC Asia Regional Hefei Online J.Queue
- 关于android下的raw文件的问题
- ocp-v13-079
- ocp-v13-080
- 压电效应和压电陶瓷
- java调用英飞拓摄像头监控接口打开摄像头,抓拍图片
- 空净风口已来 谁能“飞得更高”?
- 裴波那契数列 C++版