Problem statement Given a binary tree, determine if it is height-balanced.(link) 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: tr...
To see the question, click here. Naive Approach The idea is to determine the tree's height and then, for each level from 1 to the tree's height, gather the nodes at that level and add them to the result list. // TC: O(nh) // SC: O(n) import java.uti...
class Solution { public boolean ans = true; public int checkHeightDiff(TreeNode node){ if(node == null) return 0; int leftht = checkHeightDiff(node.left); int rightht = checkHeightDiff(node.right); if(Math.a...
We know that a tree is a non-linear data structure that represents hierarchical data and contains no cycles. Now, let's delve into a commonly asked question during interviews: When do we call a tree a binary tree, and how does it differ from a binary...
Definition Lowest Common Ancestor, LCA. LCA is an algorithm or concept used in tree structures to find the closest common ancestor of two nodes. Example In the above binary tree, the LCA of 4 and 6 is 1. LCA(4, 6) = 1 Purpose The purpose of the LCA...
Introduction 🧑🏽💻 Welcome to the fascinating world of binary trees! If you're new to this topic or looking to reinforce your understanding, you're in the right place. In this tutorial, I'll take you on a step-by-step journey through the fundamenta...