1 题目
https://leetcode-cn.com/problems/balanced-binary-tree/
2 Java
2.1 方法一(两层递归)
先写出求某个节点的层数的方法layerNum,这是第一层递归
再写出求某个节点是否平衡的方法isBalanced,这是第二层递归
某节点层数 = max(左节点层数,右节点层数)+ 1
某节点平衡 = 本身平衡 & 左节点平衡 & 右节点平衡
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
return (Math.abs(height(root.left) - height(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
}
public int height(TreeNode root){
if(root == null) return 0;
return Math.max(height(root.left), height(root.right)) + 1;
}
}
2.2 方法二(递归优化,建备忘录)
用HashMap记录每个节点高度,但是运行速度变慢了,不知道什么情况
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
// 建立备忘录
HashMap<TreeNode, Integer> treeMap = new HashMap<TreeNode, Integer>();
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
return (Math.abs(height(root.left) - height(root.right)) <= 1) && isBalanced(root.left) && isBalanced(root.right);
}
public int height(TreeNode root){
if(root == null) return 0;
if(treeMap.containsKey(root)) return treeMap.get(root);
int ans = Math.max(height(root.left), height(root.right)) + 1;
treeMap.put(root, ans);
return ans;
}
}
2.3 方法三(递归再改进)
如果非平衡,返回-1,不再判断了
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) return true;
if(height(root) == -1) return false;
return true;
}
public int height(TreeNode root){
if(root == null) return 0;
int left = height(root.left);
int right = height(root.right);
if(left == -1 || right == -1 || Math.abs(left - right) > 1) return -1;
return Math.max(left, right) + 1;
}
}
3 二刷
3.1 方法一(借助成员变量)
用一个成员变量记录是否存在非平衡的子树,这样只需要求一次树高
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
height(root);
return flag;
}
// 用一个成员变量记录是否存在非平衡的子树
boolean flag = true;
public int height(TreeNode root){
if(root == null) return 0;
int lH = height(root.left);
int rH = height(root.right);
int H = Math.max(lH, rH) + 1;
// 仅仅多了这一步!!!记录是否为平衡树
if(Math.abs(lH - rH) > 1) flag = false;
// 依然返回树高
return H;
}
}
4 三刷
4.1 方法一(借助特殊返回值 -1)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
int h = height(root);
return h == -1 ? false : true;
}
// 约等于求二叉树高度/深度,仅多了一行;面试题55 - I. 二叉树的深度(104. 二叉树的最大深度)
public int height(TreeNode root){
if(root == null) return 0;
int lH = height(root.left);
int rH = height(root.right);
// 仅多这一行,利用返回特殊值 -1
if(lH == -1 || rH == -1 || Math.abs(lH - rH) > 1) return -1;
int h = Math.max(lH, rH) + 1;
return h;
}
}
4.2 方法二(借助成员变量)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
height(root);
return res;
}
public boolean res = true;
// 约等于求二叉树高度/深度,仅多了一行;面试题55 - I. 二叉树的深度(104. 二叉树的最大深度)
public int height(TreeNode root){
if(root == null) return 0;
int lH = height(root.left);
int rH = height(root.right);
// 仅多这一行,利用成员变量记录
if(Math.abs(lH - rH) > 1) res = false;
int h = Math.max(lH, rH) + 1;
return h;
}
}