leetcode-110。平衡型二分木

件名:https : //leetcode-cn.com/problems/balanced-binary-tree/

回答:

再帰

public boolean isBalanced(TreeNode root){

        if(root == null)はtrueを返します。

        if(Math.abs(maxDepth(root.left)-maxDepth(root.right))> 1){

            falseを返します。

        }そうしないと{

            isBalanced(root.left)&& isBalanced(root.right);

        }

    }

 

      public int maxDepth(TreeNode root){

 

        if(root == null){

 

            0を返します。

 

        }そうしないと {

 

            int heightLeft = 1 + maxDepth(root.left);

 

            int heightRight = 1 + maxDepth(root.right);

 

            Math.max(heightLeft、heightRight);を返します。

 

        }

 

    }

おすすめ

転載: blog.csdn.net/wuqiqi1992/article/details/108343184