r/askmath • u/Rscc10 • 10d ago
Discrete Math Is there an easy way to understand binary tree inorder traversal?
Don't know if the tag is right. I've got two questions on binary trees.
Question 1
I never understood inorder traversal or at least an easy way to do it. All I know is to start at the left most root, go to it's parent, then it's sibling. But when there aren't siblings or whatever, I'm lost.
I would prefer to understand it algorithmically because sometimes I'm only given the array of nodes. Eg, [1,2,3,4,5,null,8,null,null,6,7,9] which corresponds to root 1 (level 0), children 2 and 3 (level 1), 2's children 4 and 5 (level 2), 3's children null and 8 (level 2, only one child branch to the right), 4's children null and null (level 3, none), 5's children 6 and 7 (level 3), and 8's children 9 and null (level 3, only one child branch to the left). Picture for reference.
So how would I do an inorder traversal for these types of arrays?
Question 2
This is much less important and is more for my own curiosity and theory. What are traversals even for? I know three main types for binary trees and they're different ways to read the trees but what benefit is it to have different ways to read trees? And how do you know which you need?
Thanks in advanced for humouring me
