c_cpp Balanced Binary Treehttp://oj.leetcode.com/problems/balanced-binary-tree/
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp Balanced Binary Treehttp://oj.leetcode.com/problems/balanced-binary-tree/相关的知识,希望对你有一定的参考价值。
/**
* 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) {
if(root == NULL){
return true;
}
if(depth(root) != -1){
return true;
}
else{
return false;
}
}
int depth(TreeNode *root){
if(root == NULL){
return 0;
}
if(root->left == NULL && root->right == NULL){
return 1;
}
int left = depth(root->left);
int right = depth(root->right);
if(left == -1 || right == -1 || left - right > 1 ||right - left > 1){
return -1;
}
else{
return (left > right) ? left + 1 : right + 1;
}
}
};
以上是关于c_cpp Balanced Binary Treehttp://oj.leetcode.com/problems/balanced-binary-tree/的主要内容,如果未能解决你的问题,请参考以下文章
Balanced Binary Tree
[Leetcode] Balanced Binary Tree
110. Balanced Binary Tree
LC.110. Balanced Binary Tree
Leetcode[110]-Balanced Binary Tree
110. Balanced Binary Tree