LeetCode-110. Balanced Binary Tree [C++][Java]https://leetcode.com/problems/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 left and right subtrees of every node differ in height by no more than 1.
Example 1:
Input: root = [3,9,20,null,null,15,7] Output: true
Example 2:
Input: root = [1,2,2,3,3,null,null,4,4] Output: false
Example 3:
Input: root = [] Output: true
Constraints:
- The number of nodes in the tree is in the range
[0, 5000]
. -104 <= Node.val <= 104
解题思路
【C++解法】
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
1、前序遍历
从上到下遍历,
借助一个获取树深度的递归函数,根据该结点的左右子树高度差判断是否平衡,然后递归地对左右子树进行判断。在判断上层结点的时候,会多次重复遍历下层结点,增加了不必要的开销。
class Solution {
public:
bool isBalanced(TreeNode* root) {
if(root == NULL) {return true;}
return abs(dfs(root->left) - dfs(root->right)) <= 1
&& isBalanced(root->left)
&& isBalanced(root->right);
}
int dfs(TreeNode* root){
return root ? 1 + max(dfs(root->left), dfs(root->right)) : 0;
}
};
2、后序遍历
从下往上遍历,
如果子树是平衡二叉树,则返回子树的高度;如果发现子树不是平衡二叉树,则直接停止遍历,这样至多只对每个结点访问一次。
两个关键步骤:
1)检查左右子树高度差;
2)判断子树是否为平衡树。
两个返回值,一个return,一个引用。
class Solution {
public:
bool isBalanced(TreeNode* root) {
int depth = 0;
return isBalanced(root, depth);
}
bool isBalanced(TreeNode* root, int& depth) {
if (root) {
int left = 0, right = 0;
if (isBalanced(root->left, left) && isBalanced(root->right, right)) {
int diff = abs(left - right);
if (diff > 1) {return false;}
depth = max(left, right) + 1;
return true;
} else {return false;}
} else {return true;}
}
};
再整洁一点
class Solution {
public:
bool isBalanced(TreeNode* root) {
return dfs(root) != -1;
}
int dfs(TreeNode* root){
if (root == nullptr) {return 0;}
int left = dfs(root->left);
int right = dfs(root->right);
if (left == -1 || right == -1 || abs(left-right) > 1) {return -1;}
else {return 1 + max(left, right);}
}
};
【Java解法】
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
1、前序遍历
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) {return true;}
return Math.abs(dfs(root.left) - dfs(root.right)) <= 1
&& isBalanced(root.left)
&& isBalanced(root.right);
}
private int dfs(TreeNode root){
return root == null ? 0 : 1 + Math.max(dfs(root.left), dfs(root.right));
}
}
2、后序遍历
class Solution {
public boolean isBalanced(TreeNode root) {
return dfs(root) == -1 ? false : true;
}
int dfs(TreeNode root){
if (root == null) {return 0;}
int l = dfs(root.left);
int r = dfs(root.right);
if (l == -1 || r == -1) {return -1;}
if (Math.abs(l-r) > 1) {return -1;}
return Math.max(l,r) + 1;
}
}