From 20ea425c1b3e5a09a415b0e153a8c819e4e2b90f Mon Sep 17 00:00:00 2001 From: yaswanthkurri Date: Thu, 11 Dec 2025 14:15:41 +0530 Subject: [PATCH] Add LeetCode 110 Balanced Binary Tree solution (C++) and contributor info --- C++/balanced_binary_tree.cpp | 30 ++++++++++++++++++++++++++++++ README.md | 2 ++ 2 files changed, 32 insertions(+) create mode 100644 C++/balanced_binary_tree.cpp diff --git a/C++/balanced_binary_tree.cpp b/C++/balanced_binary_tree.cpp new file mode 100644 index 0000000..a7ec46f --- /dev/null +++ b/C++/balanced_binary_tree.cpp @@ -0,0 +1,30 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + +int height(TreeNode* root){ + if(root==NULL) return 0; + int l=height(root->left); + if(l==-1) return -1; + int r=height(root->right); + if(r==-1) return -1; + if(abs(l-r)>1) return -1; + return 1+max(l,r); +} + bool isBalanced(TreeNode* root) { + if(root==NULL){ + return true; + } + return height(root)!=-1; + } +}; \ No newline at end of file diff --git a/README.md b/README.md index 85accc7..54e577a 100644 --- a/README.md +++ b/README.md @@ -517,6 +517,8 @@ DISCLAIMER: This above mentioned resources have affiliate links, which means if | [Shreyas Shrawage](https://github.com/shreyventure)
| India | Python | [CodeChef](https://www.codechef.com/users/shreyventure)
[LeetCode](https://leetcode.com/shreyventure/)
[HackerRank](https://www.hackerrank.com/shreyas_shrawage) | [Surbhi Mayank](https://github.com/surbhi2408)
| India | C++ | [GitHub](https://github.com/surbhi2408) | [Amrit Kumar](https://github.com/amrit-GH23)
| India | C++ | [CodeChef](https://www.codechef.com/users/amrit_kumar08)
[Linkedin](https://www.linkedin.com/in/amrit-kumar-28053b253/) +| [Yaswanth](https://github.com/yaswanthkurri)
| India | C++ | [GitHub](https://github.com/yaswanthkurri) | +
⬆️ Back to Top