美文网首页
110. Balanced Binary Tree

110. Balanced Binary Tree

作者: becauseyou_90cd | 来源:发表于2018-08-01 08:16 被阅读0次

    https://leetcode.com/problems/balanced-binary-tree/description/

    解题思路:
    用前序判断树的高度,然后对每一步判断其左右子树高度差是否大于1

    class Solution {
    boolean res = true;
    public boolean isBalanced(TreeNode root) {
    preorder(root);
    return res;
    }
    public int preorder(TreeNode root){
    if(root == null) return 0;
    int left = preorder(root.left);
    int right = preorder(root.right);
    if(Math.abs(left - right) > 1) res = false;
    return 1 + Math.max(left, right);
    }
    }

    相关文章

      网友评论

          本文标题:110. Balanced Binary Tree

          本文链接:https://www.haomeiwen.com/subject/btqlvftx.html