C# recursive solution


  • 0
    K
    public class Solution {
        public bool IsBalanced(TreeNode root) {
            
            if (root == null) return true;
            
            return IsBalancedNode(root) && IsBalanced(root.right) && IsBalanced(root.left);
    
        }
        
        public bool IsBalancedNode(TreeNode root)
        {
            if (Math.Abs(NumLevels(root.right)-NumLevels(root.left)) > 1)
                return false;
            else return true;
        }
        
        public int NumLevels(TreeNode root)
        {
            if (root == null) return 0;
            
            return Math.Max(NumLevels(root.right),NumLevels(root.left))+1;
        }
    }
    

Log in to reply
 

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