**LeetCode Problem | LeetCode Problems For Beginners | LeetCode Problems & Solutions | Improve Problem Solving Skills | LeetCode Problems Java | LeetCode Solutions in C++**

Hello **Programmers/Coders,** Today we are going to share ** solutions to the Programming problems of LeetCode Solutions in C++, Java, & Python**. At Each Problem with Successful submission with

**all Test Cases Passed,**you will get a score or marks and LeetCode Coins. And after solving maximum problems, you will be getting stars. This will highlight your profile to the recruiters.

In this post, you will find the solution for the **Balanced Binary Tree** **in C++, Java & Python-LeetCode problem**. We are providing the **correct and tested solutions** to coding problems present on **LeetCode**. If you are not able to solve any problem, then you can take help from our Blog/website.

Use “Ctrl+F” To Find Any Questions Answer. & For Mobile User, You Just Need To Click On Three dots In Your Browser & You Will Get A “Find” Option There. Use These Option to Get Any Random Questions Answer.

**About LeetCode**

*LeetCode* is one of the most well-known online judge platforms to help you enhance your skills, expand your knowledge and prepare for technical interviews.

LeetCode is for **software engineers who are looking to practice technical questions and advance their skills**. Mastering the questions in each level on LeetCode is a good way to prepare for technical interviews and keep your skills sharp. They also have a repository of solutions with the reasoning behind each step.

LeetCode has over 1,900 questions for you to practice, covering many different programming concepts. Every coding problem has a classification of either *Easy*, *Medium*, or *Hard*.

**LeetCode problems focus on algorithms and data structures. Here is some topic you can find problems on LeetCode:**

- Mathematics/Basic Logical Based Questions
- Arrays
- Strings
- Hash Table
- Dynamic Programming
- Stack & Queue
- Trees & Graphs
- Greedy Algorithms
- Breadth-First Search
- Depth-First Search
- Sorting & Searching
- BST (Binary Search Tree)
- Database
- Linked List
- Recursion, etc.

Leetcode has a huge number of test cases and questions from interviews too like Google, Amazon, Microsoft, Facebook, Adobe, Oracle, Linkedin, Goldman Sachs, etc. LeetCode helps you in getting a job in Top MNCs. To crack FAANG Companies, LeetCode problems can help you in building your logic.

** Link for the Problem** – Balanced Binary Tree– LeetCode Problem

Balanced Binary Tree– LeetCode Problem

**Problem:**

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as:

a binary tree in which the left and right subtrees of

everynode differ in height by no more than 1.

**Example 1:**

Input:root = [3,9,20,null,null,15,7]Output:true

**Example 2:**

Input:root = [1,2,2,3,3,null,null,4,4]Output:false

**Example 3:**

Input:root = []Output:true

**Constraints:**

- The number of nodes in the tree is in the range
`[0, 5000]`

. `-10`

^{4}<= Node.val <= 10^{4}

Balanced Binary Tree– LeetCode Solutions

Balanced Binary Tree Solution in C++:

class Solution { public: bool isBalanced(TreeNode* root) { return maxDepth(root) != -1; } private: // return height of root if balanced; otherwise, return -1 int maxDepth(TreeNode* root) { if (!root) return 0; const int leftHeight = maxDepth(root->left); if (leftHeight == -1) return -1; const int rightHeight = maxDepth(root->right); if (rightHeight == -1) return -1; if (abs(leftHeight - rightHeight) > 1) return -1; return 1 + max(leftHeight, rightHeight); } };

Balanced Binary Tree Solution in Java:

class Solution { public boolean isBalanced(TreeNode root) { return maxDepth(root) != -1; } // return height of root if balanced; otherwise, return -1 private int maxDepth(TreeNode root) { if (root == null) return 0; final int leftHeight = maxDepth(root.left); if (leftHeight == -1) return -1; final int rightHeight = maxDepth(root.right); if (rightHeight == -1) return -1; if (Math.abs(leftHeight - rightHeight) > 1) return -1; return 1 + Math.max(leftHeight, rightHeight); } }

Balanced Binary Tree Solution in Python:

class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: if not root: return True def maxDepth(root: Optional[TreeNode]) -> int: if not root: return 0 return 1 + max(maxDepth(root.left), maxDepth(root.right)) return abs(maxDepth(root.left) - maxDepth(root.right)) <= 1 and self.isBalanced(root.left) and self.isBalanced(root.right)