Welcome to my personal website. I use this space to share some of my thoughts and software that I write in my free time. Sometimes I also publish photos. If you want to get in touch please use LinkedIn or Mastodon.
Given two binary trees, p and q, return True if they are the same, that is, if all values are the same and False otherwise.
The easiest solution I could come up with is to use recursion. A few edge cases need to be handled.
Left or right can be null at any time. In this case it’s clear that the tree cannot be the same. If both nodes are null, the trees are the same.
Beats 87.04% of users with Python3. Beats 87.04% of users with Python3
About the author
Friedrich Ewald is an experienced Software Engineer with a Master's degree in Computer Science. He started this website in late 2015, mostly as a digital business card. He is interested in Go, Python, Ruby, SQL- and NoSQL-databases, machine learning and AI and is experienced in building scalable, distributed systems and micro-services at multiple larger and smaller companies.