Balanced binary tree
Title description
Enter a binary tree to determine whether the binary tree is a balanced binary tree.
- Here, we only need to consider its balance, not whether it is a sorted binary tree
title link : balanced binary tree
Code
/**
* 标题:平衡二叉树
* 题目描述
* 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
* 在这里,我们只需要考虑其平衡性,不需要考虑其是不是排序二叉树
* 题目链接:
* https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId=13&&tqId=11192&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz39 {
private boolean isBalanced = true;
public boolean isBalanced_Solution(TreeNode root) {
height(root);
return isBalanced;
}
/**
* 递归
*
* @param root
* @return
*/
private int height(TreeNode root) {
if (root == null || !isBalanced) {
return 0;
}
int left = height(root.left);
int right = height(root.right);
if (Math.abs(left - right) > 1) {
isBalanced = false;
}
return 1 + Math.max(left, right);
}
public static void main(String[] args) {
}
}
[Daily Message] Be a sunny person. Not sad, not impatient. Be strong, upward, and close to the sun.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。