Balanced Binary Tree
Prereq: DFS Fundamentals
A balanced binary tree is defined as a tree such that either it is an empty tree, or both its subtree are balanced and has a height difference of at most 1.
In that case, given a binary tree, determine if it's balanced.
Parameter
tree
: A binary tree.
Result
- A boolean representing whether the tree given is balanced.
Examples
Example 1
Input:
Output: true
Explanation: By definition, this is a balanced binary tree.
Example 2
Input:
Output: false
Explanation: The subtrees of the node labelled 3
has a height difference of 2
, so
it is not balanced.
Try it yourself
Solution
Title
Script
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
Ipsum
has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Contrary to popular belief, Lorem
Ipsum
is not simply random text.
1 >>> a = [1, 2, 3] 2 >>> a[-1] 3 3