leetcode 110. Balanced Binary Tree
2016-10-24 12:27
369 查看
解题思路:
递归判断左子树的高度与右子树的高度之差是否大于1
原题目:
AC解,C++代码,菜鸟一个,请大家多多指正
递归判断左子树的高度与右子树的高度之差是否大于1
原题目:
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.
AC解,C++代码,菜鸟一个,请大家多多指正
/** * 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 treeHeight(TreeNode* root) { if (root == NULL) { return 0; } if (root->left == NULL && root->right == NULL) { return 1; } return 1 + max(treeHeight(root->left), treeHeight(root->right)); } bool isBalanced(TreeNode* root) { if (root == NULL) { return true; } int diff = treeHeight(root->left) - treeHeight(root->right); if (diff >= -1 && diff <= 1) { return isBalanced(root->left) && isBalanced(root->right); } return false; } };
相关文章推荐
- Leetcode-标签为Tree 110. Balanced Binary Tree
- leetCode 110.Balanced Binary Tree (平衡二叉树) 解题思路和方法
- 【LeetCode】110. Balanced Binary Tree(Easy)解题报告
- Leetcode 110. Balanced Binary Tree
- 【LeetCode】110. Balanced Binary Tree
- 【leetcode】110. Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- leetcode--110. Balanced Binary Tree
- leetcode 110. Balanced Binary Tree
- leetcode 110. Balanced Binary Tree
- [LeetCode]110. Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree
- 【LeetCode】110. Balanced Binary Tree-判断是否为平衡二叉树
- Leetcode 110. Balanced Binary Tree 平衡二叉树 解题报告
- 【Leetcode】110. Balanced Binary Tree
- Leetcode 110. Balanced Binary Tree
- LeetCode 110. Balanced Binary Tree(平衡二叉树)
- LeetCode 110. Balanced Binary Tree
- Leetcode-110. Balanced Binary Tree
- Leetcode 110. Balanced Binary Tree