本篇文章主要介绍了" leetCode 110 Balanced Binary Tree 平衡二叉树",主要涉及到方面的内容,对于C/C++jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播感兴趣的同学可以参考一下:
110. Balanced Binary TreeGiven a binary tree, determine if it is height-balanced...
110. 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.
题目大意:
判断一颗二叉树是否为平衡二叉树。
思路:
做一个辅助函数来求的树的高度。
通过辅助函数来递归求解。
代码如下:
/**
* 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:
int depth(TreeNode* root)
{
if(!root)
return 0;
int l = depth(root->left) ;
int r = depth(root->right) ;
return 1 + ((l > r)?l:r);
}
bool isBalanced(TreeNode* root) {
if(!root)
return true;
else
{
int l = depth(root->left);
int r = depth(root->right);
if(l + 1 < r || r + 1 left) && isBalanced(root->right) );
}
}
};
2016-08-08 00:25:26
以上就介绍了 leetCode 110 Balanced Binary Tree 平衡二叉树,包括了方面的内容,希望对C/C++jrs看球网直播吧_低调看直播体育app软件下载_低调看体育直播有兴趣的朋友有所帮助。
本文网址链接:http://www.codes51.com/article/detail_3147455.html