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 depth of the two subtrees of every node never differ by more than 1.

在求高度的过程中判断是否为平衡树。。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
boolean flag=true;
public boolean isBalanced(TreeNode root) {
Depth(root);
return flag;
} //求高度,在此过程中判断是否平衡。
public int Depth(TreeNode node){
if(node==null) return 0;
int left=Depth(node.left);
int right=Depth(node.right);
if(Math.abs(left-right)>1)
flag=false;
return Math.max(left,right)+1;
}
}
上一篇:函数参数未使用警告去除 unused parameter


下一篇:配置 eslint 去掉 no-unused-vars 报错 方法汇总