Skip to content

Latest commit

 

History

History
101 lines (86 loc) · 3.11 KB

110_平衡二叉树.md

File metadata and controls

101 lines (86 loc) · 3.11 KB
import java.util.*;


/**
 * @author : xfhy
 * Create time : 2020年09月12日20:21:31
 * Description : 110. 平衡二叉树
 * source : https://leetcode-cn.com/problems/balanced-binary-tree/
 */
public class Solution {

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

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

    //-----------------------------------方案1--------------------------

    /**
     * 思路: 递归判断每个节点左边的高度是否与右边的高度相差1
     */
    public static boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        //如果左右高度差值大于1 则不行
        if (Math.abs(getTreeHeight(root.left) - getTreeHeight(root.right)) > 1) {
            return false;
        } else {
            //判断左边和右边
            return isBalanced(root.left) && isBalanced(root.right);
        }
    }

    public static int getTreeHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getTreeHeight(root.left), getTreeHeight(root.right)) + 1;
    }

    //-----------------------------------方案2--------------------------

    /**
     * 思路2: 方案1中同一个节点会调用getTreeHeight方法多次.这次咱一个节点只调一次getTreeHeight方法,
     * 先递归的判断某个节点左右子树是否平衡,再判断以当前节点为根的子树是否平衡.
     */
    public static boolean isBalanced2(TreeNode root) {
        if (root == null) {
            return true;
        }
        return getTreeHeight2(root) > 0;
    }

    public static int getTreeHeight2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight2(root.left);
        int rightHeight = getTreeHeight2(root.right);
        if (leftHeight == -1 || rightHeight == -1 || Math.abs(leftHeight - rightHeight) > 1) {
            return -1;
        } else {
            return Math.max(leftHeight, rightHeight) + 1;
        }
    }

    public static TreeNode createBinaryTree(LinkedList<Integer> inputList) {
        if (inputList == null || inputList.isEmpty()) {
            return null;
        }
        TreeNode node = null;
        Integer data = inputList.removeFirst();
        if (data != null) {
            node = new TreeNode(data);
            node.left = createBinaryTree(inputList);
            node.right = createBinaryTree(inputList);
        }
        return node;
    }

    public static void main(String[] args) {
        /*LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(5, 4, 11, 7, null, null, 2, null, null, null, 8, 13, null, null, 4,
                5, null, null, 1));*/
        /*LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(3, 2, 9, null, null, 10, null,
                null, 8, null, 4));*/
        LinkedList<Integer> integers = new LinkedList<>(Arrays.asList(3, 9, null, null, 20, 15, null, null, 7));
        TreeNode binaryTree = createBinaryTree(integers);

        System.out.println(isBalanced2(binaryTree));
    }

}