# Binary Tree Traversals and call stack during that time?

**URL:** <https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525>\
**Category:** Uncategorized\
**Created:** [October 3, 2025, 9:04am UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525 "2025-10-03T09:04:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [October 3, 2025, 9:04am UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/1 "2025-10-03T09:04:20Z")

</div>

```auto
       60
      / \
    55 100
   / \ / \
 45 57 67 107
       \ /
       59 101

```

The binary tree to be traversed is as provided above.

The pre-order traversal pseudocode is as presented below:

```auto
if(root==NULL) return;
print(root->data);
preorder(root->left);
preorder(root->right);

```

During the preorder traversal of this binary search tree, I wanted to know how the call stack works.

Initially, root is 60. As soon as the root arrives, it gets printed as per the pre-order traversal algorithm. So root and its left and right children are pushed onto the stack.

```auto
+------+-----------+------------+
| root | root->left| root->right|
+------+-----------+------------+
| 60 | 55 | 100 |
+------+-----------+------------+

```

Then, preorder(55) is called. Immediately 55 gets printed and 55,45,57 gets pushed onto the stack(I mean values are recorded somewhere, for simplicity I assume it is stack).

```auto
+------+-----------+------------+
| root | root->left| root->right|
+------+-----------+------------+
| 55 | 45 | 57 |
| 60 | 55 | 100 |
+------+-----------+------------+

```

Then preorder(45) is called. Immediately 45 gets printed and 45,NULL,NULL is pushed onto the stack.

```auto
+------+-----------+------------+
| root | root->left| root->right|
+------+-----------+------------+
| 45 | NULL | NULL |
| 55 | 45 | 57 |
| 60 | 55 | 100 |
+------+-----------+------------+

```

Since root-\>left=NULL, top of the stack must be popped and function call returns to preorder(root-\>right) i.e. root=57 onwards

57 is pushed onto the stack with two NULL values and immediately been printed.

```auto
+------+-----------+------------+
| root | root->left| root->right|
+------+-----------+------------+
| 57 | NULL | NULL |
| 55 | 45 | 57 |
| 60 | 55 | 100 |
+------+-----------+------------+

```

Now what should happen? And could you correct me if I was wrong?

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [October 3, 2025, 12:46pm UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/2 "2025-10-03T12:46:05Z")

</div>

What you are doing seems to be the equivalent of a depth-first search:

> **[Depth-First Search (DFS) and Breadth-First Search (BFS)](https://www.kirupa.com/data_structures_algorithms/dfs_bfs.htm)**
>
> Learn all about how Depth-First Search (DFS) and Breadth-First Search (BFS) will allow you to fully traverse and explore a graph. Yes, you also have a fully functioning JavaScript implementation.

One change to call out is that your stack will only contain the node you are about to explore. It won’t contain a preview of future stacks or include null values.

---

<div class="post-metadata">

**Author:** ![hungryfox092](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@hungryfox092](https://forum.kirupa.com/u/hungryfox092)\
**Post date:** [December 10, 2025, 10:43am UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/3 "2025-12-10T10:43:42Z")

</div>

thanks for sharing..!

---

<div class="post-metadata">

**Author:** ![HARRYJOSAN](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/harryjosan/32/30318_2.png) [@HARRYJOSAN](https://forum.kirupa.com/u/HARRYJOSAN)\
**Post date:** [December 13, 2025, 6:23am UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/4 "2025-12-13T06:23:28Z")

</div>

Your step-by-step breakdown of the preorder traversal and call stack is excellent! You’re correctly visualizing how each function call is pushed onto the stack, and how left children are processed before right children. After processing 57, since its left and right children are NULL, the stack will pop back to 60, and then preorder root-\>right for node 100 continues. Your understanding is solid; just remember the stack always mirrors the function call hierarchy.

---

<div class="post-metadata">

**Author:** ![polaryeti](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/polaryeti/32/19283_2.png) [@polaryeti](https://forum.kirupa.com/u/polaryeti)\
**Post date:** [January 22, 2026, 6:58am UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/5 "2026-01-22T06:58:06Z")

</div>

confused.

---

<div class="post-metadata">

**Author:** ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)\
**Post date:** [January 22, 2026, 11:56pm UTC](https://forum.kirupa.com/t/binary-tree-traversals-and-call-stack-during-that-time/677525/6 "2026-01-22T23:56:56Z")

</div>

What part is confusing? If you share more, I can see if I can help un-confuse you 🙂
