输入一棵二叉树,判断该二叉树是否是平衡二叉树。

import java.util.ArrayList;
import java.util.Collections;

public class Solution {
    public class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;

        public TreeNode(int val) {
            this.val = val;

        }

    }
      public boolean IsBalanced_Solution(TreeNode root) {
          if(root == null) return true;
          int leftHeight = getTreeHeight(root.left);
          int rightHeight = getTreeHeight(root.right);
          if(leftHeight - rightHeight >1 || leftHeight - rightHeight <-1)
              return false;
          return IsBalanced_Solution(root.left) && IsBalanced_Solution(root.right);        
        }






    private int getTreeHeight(TreeNode root) {
         if(root == null) return 0;
            int leftLen = getTreeHeight(root.left);
            int rightLen = getTreeHeight(root.right);
            return leftLen > rightLen ? leftLen + 1 : rightLen + 1;
    }
    public static void main(String[] args) {

    }
}
    原文作者:平衡二叉树
    原文地址: https://blog.csdn.net/u012324780/article/details/51069585
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞