一、题目内容
二、题解
解法1:
思路
左子树深度与右子树深度绝对值小于等于1,同时左右子树都为平衡树,dfs
代码
public class Solution {public boolean IsBalanced_Solution(TreeNode root) {if(root == null){return true;}int leftDepth = depth(root.left);int rightDepth = depth(root.right);return IsBalanced_Solution(root.left) &&IsBalanced_Solution(root.right) &&Math.abs(leftDepth - rightDepth) <= 1;}private int depth(TreeNode root){if(root == null){return 0;}return Math.max(depth(root.left), depth(root.right)) + 1;}}
