OneCoder Avatar
OneCodercoderli.com · 937 篇博文
{ } Java

Java 服务端架构

Spring、Netty、日志框架与工程化实战

🎨 视觉封面

LeetCode Balanced Binary Tree

📅 2017-11-23·✍️ onecode·计算中...·⏱️ 6 分钟
#LeetCode#Java

Problem

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.

即判断一个二叉树是不是平衡树。平衡的标准就是任何节点的左右子树的高度差不大于1。

Java 实现

JAVA

package com.coderli.leetcode.algorithms.easy;

/**
 * Given a binary tree, determine if it is height-balanced.
 * <p>
 * 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.
 *
 * @author OneCoder 2017-11-23 22:05
 */
public class BalancedBinaryTree {

    public static void main(String[] args) {
        BalancedBinaryTree balancedBinaryTree = new BalancedBinaryTree();
        TreeNode tree = balancedBinaryTree.new TreeNode(1);
        TreeNode subNodeLeft = balancedBinaryTree.new TreeNode(2);
        TreeNode subNodeRight = balancedBinaryTree.new TreeNode(2);
        subNodeLeft.left = balancedBinaryTree.new TreeNode(3);
        subNodeLeft.right = balancedBinaryTree.new TreeNode(4);
        subNodeRight.left = balancedBinaryTree.new TreeNode(4);
        subNodeRight.right = balancedBinaryTree.new TreeNode(3);
        tree.left = subNodeLeft;
        tree.right = subNodeRight;
        System.out.println(balancedBinaryTree.isBalanced(tree));
    }

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        int leftSubTreeHeight = treeHeight(root.left);
        int rightSubTreeHeight = treeHeight(root.right);
        int differ = leftSubTreeHeight - rightSubTreeHeight;
        if ( differ > 1 || differ < -1) {
            return false;
        }
        return isBalanced(root.left) && isBalanced(root.right);
    }

    private int treeHeight(TreeNode tree) {
        if (tree == null) {
            return 0;
        }
        int leftSubHeight = treeHeight(tree.left);
        int rightSubHeight = treeHeight(tree.right);
        return leftSubHeight >= rightSubHeight ? leftSubHeight + 1: rightSubHeight + 1;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}


分析

还是递归处理。一个递归计算高度。另一个递归计算每个节点的高度,并进行判断。

💡 OneCoder 资源指引

所有代码开源上传至 GitHub:yummy-code 仓库 · GESP 专题站:GESP WIKI

🤝 技术交流与答疑

欢迎加入:C++ GESP/CSP 考级答疑群(688906745)Java/Python交流群(982860385),点击可直接加群。

📚

猜你想读 · 相关文章推荐

OneCoder

OneCoder (lihongzheshuai)

一个中年人的自留地,记录学习 C++、GESP/NOI、Java、Python 与算法架构的心得体会。本站唯一网址:coderli.com

💬 读者留言与交流

0 条讨论
✨ 支持 Markdown 语法格式
还没有留言,快来成为第一个讨论者吧!