leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4359a7cf6ee7bd279e92d1a1422660a0456e5053
parent fe75d78dc5802107587993036061f9e2d99b9d0e
Author: Andrew Laack <andrew@laack.co>
Date:   Thu, 26 Jun 2025 21:00:31 -0500

Finished another graph problem

Diffstat:
Abalanced-binary-tree/balanced-binary-tree.py | 17+++++++++++++++++
1 file changed, 17 insertions(+), 0 deletions(-)

diff --git a/balanced-binary-tree/balanced-binary-tree.py b/balanced-binary-tree/balanced-binary-tree.py @@ -0,0 +1,17 @@ +class Solution: + def isBalanced(self, root: Optional[TreeNode]) -> bool: + return dfs(root) != -1 + +# return depth of tree +def dfs(root): + + if root is None: + return 0 + + left_depth = dfs(root.left) + right_depth = dfs(root.right) + + if abs(right_depth - left_depth) > 1 or left_depth == -1 or right_depth == -1: + return -1 + + return max(left_depth, right_depth) + 1