leetcode刷题, 总结,记录,备忘 110
来源:互联网 发布:sql把表中一列全部增值 编辑:程序博客网 时间:2024/05/16 20:28
leetcode110
Balanced Binary Tree
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.
使用递归,计算每个子树的深度,以及2个子树分别是否为平衡树,这3个条件作为判断条件,还是比较容易理解的。/** * 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 Balanced(TreeNode * root, int & depth) { if (root == NULL) { depth = 0; return true; } int ldepth, rdepth; bool left = Balanced(root->left, ldepth); bool right = Balanced(root->right, rdepth); depth = max(ldepth, rdepth) + 1; return left && right && abs(ldepth - rdepth) <= 1; } bool isBalanced(TreeNode* root) { int depth; return Balanced(root, depth); }};
0 0
- leetcode刷题, 总结,记录,备忘 110
- leetcode刷题。总结,记录,备忘 104
- leetcode刷题。总结,记录,备忘 100
- leetcode刷题。总结,记录,备忘 191
- leetcode刷题。总结,记录,备忘。168
- leetcode刷题, 总结,记录,备忘 171
- leetcode刷题 ,总结,记录,备忘。144
- leetcode刷题,总结,记录, 备忘 141
- leetcode刷题,总结,记录,备忘。116
- leetcode刷题,总结,记录,备忘。96
- leetcode刷题。记录,总结,备忘。217
- leetcode 刷题, 总结,记录,备忘 35
- leetcode刷题,总结,记录,备忘 226
- leetcode刷题,总结,记录,备忘 169
- leetcode刷题,总结,记录,备忘137
- leetcode刷题,总结,记录,备忘83
- leetcode刷题,总结,记录,备忘70
- leetcode刷题,总结,记录,备忘 108
- iOS 各种传值方式
- 高效排除网络故障 从用好Ping命令开始
- sgu156
- 什么是跨域问题
- OC基础回顾(二)面向对象编程
- leetcode刷题, 总结,记录,备忘 110
- 出其中的破绽
- 环形内存circular_buffer
- 1176_高斯求和
- poj1141根据不同规模构造最优子问题求解DP
- 燕怜秋晃了晃自己的黑木牌
- 处理一些编译警告的简单
- NS3网络仿真(7): Wifi节点
- jquery中ajax的get()与post()