Leetcode 110. Balanced Binary Tree

来源:互联网 发布:mac照片导入iphone8 编辑:程序博客网 时间:2024/06/02 04:52
public class Solution {    public boolean isBalanced(TreeNode root) {        // do an inorder traverse         if (root == null) return true;        if (Math.abs(getHeight(root.left) - getHeight(root.right)) > 1) return false;        if (!isBalanced(root.left) || !isBalanced(root.right)) return false;        return true;    }        // determine the height of a given tree t    public static int getHeight(TreeNode t) {        if (t == null) return 0;        return 1 + Math.max(getHeight(t.left), getHeight(t.right));    }}

0 0
原创粉丝点击