zoukankan      html  css  js  c++  java
  • leetcode — balanced-binary-tree

    /**
     * Source : https://oj.leetcode.com/problems/balanced-binary-tree/
     *
     *
     * 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.
     */
    public class BalancedBinaryTree {
    
        public boolean isBanlanced (TreeNode root) {
            return treeDepth(root) == -1 ? false : true;
        }
    
    
        /**
         * 判断一棵二叉树是否是高度平衡二叉树
         *
         * 求出左右子树各自的总高度,如果不是则及早返回-1
         *
         * @param root
         * @return
         */
        public int treeDepth (TreeNode root) {
            if (root == null) {
                return 0;
            }
            int leftDepth = treeDepth(root.leftChild);
            if (leftDepth == -1) {
                return -1;
            }
    
            int rightDepth = treeDepth(root.rightChild);
            if (rightDepth == -1) {
                return -1;
            }
            if (Math.abs(leftDepth-rightDepth) > 1) {
                return -1;
            }
            return Math.max(leftDepth, rightDepth) + 1;
        }
    
        public TreeNode createTree (char[] treeArr) {
            TreeNode[] tree = new TreeNode[treeArr.length];
            for (int i = 0; i < treeArr.length; i++) {
                if (treeArr[i] == '#') {
                    tree[i] = null;
                    continue;
                }
                tree[i] = new TreeNode(treeArr[i]-'0');
            }
            int pos = 0;
            for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
                if (tree[i] != null) {
                    tree[i].leftChild = tree[++pos];
                    if (pos < treeArr.length-1) {
                        tree[i].rightChild = tree[++pos];
                    }
                }
            }
            return tree[0];
        }
    
    
        private class TreeNode {
            TreeNode leftChild;
            TreeNode rightChild;
            int value;
    
            public TreeNode(int value) {
                this.value = value;
            }
    
            public TreeNode() {
    
            }
        }
    
        public static void main(String[] args) {
            BalancedBinaryTree balancedBinaryTree = new BalancedBinaryTree();
            char[] arr0 = new char[]{'#'};
            char[] arr1 = new char[]{'3','9','2','#','#','1','7'};
            char[] arr2 = new char[]{'3','9','2','#','#','1','7','5'};
            System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr0)));
            System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr1)));
            System.out.println(balancedBinaryTree.isBanlanced(balancedBinaryTree.createTree(arr2)));
        }
    
    }
    
  • 相关阅读:
    第三方支付——支付宝支付
    使用Ansible自动配置Nginx服务
    使用Ansible自动配置JDK环境
    mycat 生产环境 cpu 占用 800% 问题 Mycat调优启用useOffHeapForMerge报java.lang.NumberFormatException异常解决(附源码)
    es 备份 恢复
    修改es 副本数 replicas
    Java压缩流GZIPStream导致的内存泄露
    java 堆外内存泄漏 排查
    Linux下查看某一进程所占用内存的方法(转)
    jmap -histo java内存泄漏排查 -XX:MaxDirectMemorySize=2G
  • 原文地址:https://www.cnblogs.com/sunshine-2015/p/7812819.html
Copyright © 2011-2022 走看看