Learning GoLang: Check Same Binary Trees via DFS or BFS Algorithms
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.
GoLang: Same Tree Algorithm via Recursive Depth First Search Algorithm
Checking two binary trees for equality - we need to check two things: root values and their structures. The root values checking is easy - and we can do the recursion to check their structures - left and right trees.
The following is the Recursive Depth First Search Algorithm to check two binary trees for equality.
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
if p == nil && q == nil {
return true
}
if p == nil || q == nil {
return false
}
if p.Val != q.Val {
return false
}
return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right)
}
The time and space complexity is O(N) where N is the less number of the nodes in two binary trees i.e. O(Min(P, Q)). Using a Recursion implies the usage of Stack.
GoLang: Same Tree Algorithm via Breadth First Search Algorithm
We can also perform a Breadth First Search Algorithm via a queue - level-by-level order traversal.
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSameTree(p *TreeNode, q *TreeNode) bool {
var Q = make([][]*TreeNode, 0)
Q = append(Q, []*TreeNode{p, q})
for len(Q) > 0 {
a, b := Q[0][0], Q[0][1]
Q = Q[1:]
if a == nil && b == nil {
continue
}
if a == nil || b == nil {
return false
}
if a.Val != b.Val {
return false
}
Q = append(Q, []*TreeNode{a.Left, b.Left})
Q = append(Q, []*TreeNode{a.Right, b.Right})
}
return true
}
We store a pair of nodes in the queue - and check them for equality. If they are equal - we continue push their corresponding children into the queue. If non-equality are found, we return false, otherwise, when the BFS exits, we return the true.
Same Binary Tree Check Algorithm:
- How to Check If Two Binary Trees are the Same?
- Teaching Kids Programming – Recursive Depth First Search Algorithm to Check If Two Binary Trees are Same
- GoLang: Check Same Binary Trees via DFS or BFS Algorithms
- Teaching Kids Programming – Breadth First Search Algorithm to Check If Two Binary Trees are Same
Reposted to Blog
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Thank you for reading ^^^^^^^^^^^^^^^
NEW! Following my Trail (Upvote or/and Downvote)
Follow me for topics of Algorithms, Blockchain and Cloud.
I am @justyy - a Steem Witness
https://steemyy.com
My contributions
- Video Downloader
- Steem Blockchain Tools
- Free Cryptos API
- VPS Database
- Computing Technology Blog
- A few useless tools
- And some other online software/tools
- Merge Files/Videos
- LOGO Turtle Programming Chrome Extension
- Teaching Kids Programming - Youtube Channel and All Contents
Delegation Service
Important Update of Delegation Service!
Support me
If you like my work, please:
- Buy Me a Coffee, Thanks!
- Become my Sponsor, Thanks!
- Voting for me:
https://steemit.com/~witnesses type in justyy and click VOTE
- Delegate SP: https://steemyy.com/sp-delegate-form/?delegatee=justyy
- Vote @justyy as Witness: https://steemyy.com/witness-voting/?witness=justyy&action=approve
- Set @justyy as Proxy: https://steemyy.com/witness-voting/?witness=justyy&action=proxy
Alternatively, you can vote witness or set proxy here: https://steemit.com/~witnesses
Hello sir,
I Delegate 16000 sp, Set your username as a proxy Witness and vote you as a Witness.
Can i get upvote from you?
https://steemit.com/hive-129948/@moh.arif/2egbbs
@justyy again missed upvote....
https://steemit.com/hive-129948/@rme/six-photographs-of-flowers
done.
thank you :D