[C]LeetCode:Balanced Binary Tree
来源:互联网 发布:软件poc产品 编辑:程序博客网 时间:2024/06/06 16:32
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.
此题解法1:进行DFS递归算出两边长度,然后进行比较,依次进行上述比较的递归 得到结果
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */bool isBalanced(struct TreeNode* root) { if (root==NULL) return true; int suml=depth(root->left); int sumr=depth(root->right); if(abs(suml-sumr)>1) return false; return (isBalanced(root->left)&&isBalanced(root->right)); }int depth(struct TreeNode* nodes){ if (nodes==NULL) return 0; int suml=depth(nodes->left); int sumr=depth(nodes->right); return suml>sumr?suml+1:sumr+1;}
0 0
- [C]LeetCode:Balanced Binary Tree
- [C++]LeetCode: 35 Balanced Binary Tree
- [leetcode-110]Balanced Binary Tree(C)
- 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
- Hadoop之Secondary NameNode
- 二分求幂(pow的logn算法)
- 51nod1242 斐波那契数列的第N项
- git能不能管理文件夹
- ServletContext作用功能详解
- [C]LeetCode:Balanced Binary Tree
- 628A. Tennis Tournament
- FMDB使用
- Javascript之this对象初解
- 知也atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException v2 q31无涯 - I
- 网易公开课《Linux内核分析》学习心得-跟踪分析Linux内核的启动过程
- D9
- Android打包签名那点事
- 会员中心增加会员登录次数显示功能–PHPCMS V9