Balanced Binary Tree
来源:互联网 发布:淘宝客购物车结算 编辑:程序博客网 时间:2024/06/05 04:08
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.
/** * 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 bRes;int depth(TreeNode* root){if (0 == root) return 0;if (0 == root->left && 0 == root->right)return 1;int depthL = depth(root->left);int depthR = depth(root->right);if (abs(depthL - depthR) > 1)bRes = false;return depthL > depthR ? 1 + depthL : 1 + depthR;} bool isBalanced(TreeNode *root){bRes = true;depth(root);return bRes; }};
说点自己的感觉,一开始做这道题稍微有点想法就忙去写代码,导致提交三次都错误,后来搞明白自己一直没有一个确定的思路,然后做完另一道题后,具体扎扎实实的总结了这道题,应该用什么思路,算法,结果得出,在递归求depth过程中,比较左右子树depth之差。
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
- Sum Root to Leaf Numbers
- Fedora VMware Workstation Kernel headers install
- 第一部分C语言入门 第一章 程序的基本概念
- 【讨论思考】可不可以不用pointer而实现一个linked list?
- Android AIDL proxy/stub 分析
- Balanced Binary Tree
- Android HAL 分析
- 在Java中用引号创建String对象和用构造函数的区别
- Solve Problem: “Unable to load realm info from SCDynamicStore” on Mac OS X Mavericks 10.9.1
- ASP.NET 页面中加添加用户控件的写法
- c#异步task示例分享(异步操作)
- linux输入输出重定向使用详解(收藏)
- 较有创意的广告创意入门读物——Leo鉴书50
- 机会是留给有准备的人