Balanced Binary Tree
来源:互联网 发布:中国乒乓球怎么了知乎 编辑:程序博客网 时间:2024/05/29 08:26
检测一个树是否平衡,不需要求出高度,而是从底到顶检测是否平衡,这样才算法时间复杂度为O(n)。但是需要额外的O(logn)的空间
/** * Definition for binary tree * 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 checkBalance(root) >= 0; } int checkBalance(TreeNode *root){ if(root == NULL) return 0; int left = checkBalance(root->left); int right = checkBalance(root->right); if(left < 0 || right < 0 || abs(left-right) > 1) return -1; return max(left,right) +1; }};
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
- 谈谈对Spring IOC的理解
- Windows+Git+TortoiseGit+COPSSH 安装图文教程
- HDU-幂次方取尾数
- Openmp补遗之同步
- ClassNotFount xxx 但是maven里的jar包存在
- Balanced Binary Tree
- 2015年1月27号工作日志
- 《设计模式之禅》——门面模式
- LeetCode:Minimum Path Sum
- Audioseg在Ubuntu14.04下安装详解
- 输出多组中颜色最多的气球法一
- 小波分析和尺度函数(中)
- Python模块学习 ---- subprocess 创建子进程
- Ajax