zoukankan      html  css  js  c++  java
  • Java实现LeetCode 110. Balanced Binary Tree

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public int height(TreeNode root){
            if(root == null)
                return 0;
            int left_height = height(root.left);
            int right_height = height(root.right);
            return 1 + (left_height > right_height ? left_height : right_height);
        }
        public boolean isBalanced(TreeNode root) {
            if(root == null){
                return true;
            }
            int left_height = height(root.left);
            int right_height = height(root.right);
            if(Math.abs(left_height - right_height) > 1){
                return false;
            }
            else{
                return isBalanced(root.left) && isBalanced(root.right);
            }
        }
    }
    
  • 相关阅读:
    html常用标签_new
    Nginx缓存
    购物车
    css的属性选择
    前端基础之css
    htm基础知识
    TypeScript(1)为什么需要TypeScript
    Electron
    Ant Design
    Umi
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13076046.html
Copyright © 2011-2022 走看看