Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Written by Prashant Basnet
👋 Welcome to my Signature, a space between logic and curiosity.
I’m a Software Development Engineer who loves turning ideas into systems that work beautifully.
This space captures the process: the bugs, breakthroughs, and “aha” moments that keep me building.
What are some real world usage of this tree comparison algorithm?
1. Facebook: React's Virtual DOM:
2. Amazon: Merkle Trees in S3 and DynamoDB:
2. Google: Google Drive File Sync:
Now that we understand what's the use of this algorithm. let's look into our actual question of comparing if two binary trees given to us are same or not.
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
Derivatives:
function bfs(root){ /* we want a 1. currentNode 2. list to store the result 3. queue to process inorder next we check if we have data to process in the queue if(queue.length > 0){ let node = queue.shift() list.push(node.value) node.left && queue.push(node.left) node.right && queue.push(node.right) } return list */ const list = [] const queue = [] queue.push(root) while(queue.length > 0){ let currentNode = queue.shift() list.push(currentNode.value) currentNode.left && queue.push(currentNode.left) currentNode.right && queue.push(currentNode.right) } return list }How do we link this solution to check if the tree are identical?
To me this sounds reasonable solution??
BFS ensures that the nodes are visited level by level. However, two trees can have the same BFS result but different structures. For example:
Both trees would produce the same BFS result [1, 2], but they are structurally different.
So does it have to do anything with BFS or DFS at all?
All we know is we need to traverse all the nodes in a tree and compare to each other as we traverse.
To me if we are solving same nature of problem over and over again it is some kind of recursive problem.
Does that ring a bell? Recursive is how our DFS are designed.
We want to compare these nodes as we go through: i.e grab as you go (Pre-order)
function dfsPreOrder(node){ // list.push(node.value) dfsPreOrder(node.left) dfsPreOrder(node.right) }Now next few questions we need to tackle:
function isSameTree(pTreeNode , qTreeNode){ /* grab as you go base cases 1. if both nodes are null, it's same return true 2. if one nodes exist and other don't return false 3. if at anypoint the nodes are not equal return false */ if(!pTreeNode && !qTreeNode) return true if(!pTreeNode || !qTreeNode) return false if(pTreeNode.val !== qTreeNode.val) return false return isSameTree(pTreeNode.left, qTreeNode.left) && isSameTree(pTreeNode.right, qTreeNode.right) }Complexity:
Following up questions:
let's continue the discussion!!
#blind75 #tree #binaryTree