博客
关于我
剑指offer JZ39 平衡二叉树
阅读量:362 次
发布时间:2019-03-04

本文共 549 字,大约阅读时间需要 1 分钟。

题目链接:

JZ39

本题思路:

import java.lang.Math;public class Solution {       public boolean IsBalanced_Solution(TreeNode root) {           if(root == null) return true;        // 判断左右两个子树的高度差 && 左右两个子树都是一棵平衡二叉树        return Math.abs(maxDepth(root.left) - maxDepth(root.right)) <=1             && IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);    }            // 获取左右两个子树的最大高度    public int maxDepth(TreeNode root) {           if(root == null) return 0;                return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));    }}

转载地址:http://mune.baihongyu.com/

你可能感兴趣的文章