/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public bool IsBalanced(TreeNode root) { if(root == null || root.left == null && root.right == null){ return true; } var l = Depth(root.left); var r = Depth(root.right); if(Math.Abs(l-r) > 1){ return false; } var bLeft = IsBalanced(root.left); var bRight = IsBalanced(root.right); return bLeft && bRight; } public int Depth(TreeNode node) { if(node == null){ return 0; } var l = Depth(node.left); var r = Depth(node.right); var len = Math.Max(l,r); return len + 1; } }