题解 | #判断是不是平衡二叉树#
判断是不是平衡二叉树
https://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222
/* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } }*/ using System; class Solution { public int depth(TreeNode root) { if (root == null) return 0; if (root.left == null && root.right == null) return 1; return Math.Max(depth(root.left), depth(root.right)) + 1; } public bool IsBalanced_Solution(TreeNode pRoot) { // write code here if (pRoot == null) return true; if (Math.Abs(depth(pRoot.left) - depth(pRoot.right)) <= 1) return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right); return false ; } }#判断一颗二叉树是否是平衡二叉树#