From a25384559731d2ab4d5bca9eb90398ce58dcd6ca Mon Sep 17 00:00:00 2001 From: Snehit Roda Date: Tue, 26 Dec 2023 00:57:36 -0400 Subject: [PATCH] added 0110 balanced binary tree simplified java solution --- java/0110-balanced-binary-tree.java | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/java/0110-balanced-binary-tree.java b/java/0110-balanced-binary-tree.java index ff6da988e..5d4d7ddeb 100644 --- a/java/0110-balanced-binary-tree.java +++ b/java/0110-balanced-binary-tree.java @@ -25,3 +25,32 @@ public static boolean isBalanced(TreeNode root) { return dfs(root).getKey(); } } + +// Solution using the bottom up approach +// TC and SC is On + +class Solution { + + public int height(TreeNode root){ + if(root == null){ + return 0; + } + + int lh = height(root.left); + int rh = height(root.right); + + return 1 + Math.max(lh,rh); + } + + public boolean isBalanced(TreeNode root) { + + if(root == null){ + return true; + } + + int lh = height(root.left); + int rh = height(root.right); + + return Math.abs(lh - rh) <= 1 && isBalanced(root.left) && isBalanced(root.right); + } +} \ No newline at end of file