DEV Community

Posted on

Day 14/366

Day 14/366 of #1PercentPlusPlus

🚀 Today's Learning:


  • Height of Binary Tree
  • Determine if two trees are identical

🌟 Dev

  • Promises in JS

🔍 Some Key Highlights:


Height of Binary Tree:
To calculate the height of a binary tree, we begin traversal using DFS. At each node, the height is determined as 1 plus the maximum of the heights of its left and right subtrees. We recursively compute this for all nodes, maintaining a maxHeight variable to track the maximum height encountered.

Determine if two trees are identical:
We utilize a preorder traversal approach to determine if two trees are identical. Starting from the root, we compare corresponding nodes of both trees. If at any point the nodes differ, or if one tree has more nodes than the other, we conclude that the trees are not identical. Otherwise, we continue recursively checking the left and right subtrees until all nodes are compared. If the traversal completes without finding any differences, we affirm that the trees are identical.


Promises in JavaScript provide a way to handle asynchronous operations more effectively. Using promises follows a pattern similar to DFS traversal.

When working with promises, you initiate an asynchronous operation and get back a promise object. This object represents the eventual completion (or failure) of the operation.

You can then attach callbacks to the promise using then() method. These callbacks will be invoked when the operation succeeds or fails.

The structure is akin to preorder traversal:

  • You start by initiating the asynchronous operation.
  • Then, you attach a callback to handle the successful completion (akin to processing the current node).
  • Next, you may attach another callback to handle any errors that might occur during the operation (analogous to handling the left subtree).
  • Finally, you continue with any further processing or chaining of promises (similar to handling the right subtree).

Through this approach, promises enable more structured and manageable handling of asynchronous code in JavaScript.

#100daysofcode #1percentplusplus #coding #dsa

Top comments (0)