DEV Community

vishal.codes
vishal.codes

Posted on

Day 28/366

🚀 Today's Learning:

🌟 DSA

  • Maximum depth of binary tree
  • Check if two trees have same structure

🌟 Dev

  • Virtual DOM in React

🔍 Some Key Highlights:

DSA

To find the maximum depth of a binary tree, you start by checking if the current node is null. If it is, return 0 to indicate the end of the branch. Otherwise, recursively calculate the maximum depth of the left and right subtrees. Return the maximum of these two depths plus 1 to account for the current node. This way, as you traverse each node, you keep track of the maximum depth encountered so far.

To check if two trees have the same structure, you start by checking if both trees are null. If they are, return true to indicate that they have the same structure. If one tree is null and the other isn't, or vice versa, return false as their structures differ. Otherwise, recursively compare the left and right subtrees of both trees. If all corresponding nodes have the same structure, return true; otherwise, return false. This way, you traverse both trees simultaneously and ensure that their structures match at each level.

DEV

In React, the virtual DOM is a lightweight copy of the actual DOM. When changes occur in a React component, instead of directly updating the real DOM, React first updates the virtual DOM. React then compares the virtual DOM with the previous version to identify the minimum number of changes needed. This process, known as reconciliation, allows React to efficiently update the real DOM by only making the necessary changes. By using the virtual DOM, React optimizes performance and provides a simpler programming model for developers, as they can work with a virtual representation of the DOM rather than directly manipulating the actual DOM.

Top comments (0)