c#(sharp) accepted solution, beat 86%.


  • 0
           public static bool IsBalanced(TreeNode root)
            {
                if (root == null) return true;
                int lh = MaxHeight(root.left);
                int rh = MaxHeight(root.right);
                if (Math.Abs(lh - rh) <= 1 && IsBalanced(root.left) && IsBalanced(root.right)) return true;
                return false;            
            }
    
            public static int MaxHeight(TreeNode root)
            {
                if (root == null) return 0;
                return Math.Max(MaxHeight(root.left) + 1, MaxHeight(root.right) + 1);
            }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.