0

I am new to learning the Binary tree concept. I'm having a problem with the following:

int lDepth = maxDepth(node->left); 
int rDepth = maxDepth(node->right);

In this piece of code, what is the

"maxDepth(node->left); "

What is being stored in the 'LDepth' variable, and the same with the next line, after each recursive call - what is being stored in the 'lDepth' variable?

I would like as specific an answer as possible.

Mozahler
  • 4,958
  • 6
  • 36
  • 56
  • You need to understand the concept of recursion... A nice explanation you can fine..https://stackoverflow.com/a/717839/5236014 – mukesh kudi Oct 19 '18 at 06:36

2 Answers2

0

From the code that you have given I would say lDepth is storing the depth of the left subtree and rDepth is storing the depth of the right subtree where node is the root of the tree. This is true for all the subtree recursively.

"maxDepth(node->left);

You are calling maxDepth function recursively to compute the depth of left subtree whose root is node.

suvojit_007
  • 1,690
  • 2
  • 17
  • 23
  • i got it what you told ,But what the value like 1,2,3 or etc value is storing the lDepth variable, what is the type of value is strong in lDepth variable and HOW it is storing if it 1,2,3 ... – surendraps007 Oct 19 '18 at 12:47
  • @surendraps007 type of the value is `int` and at first, it's computing the depth of smallest subtree and then it's returning the value to compute a bigger subtree with that value recursively. – suvojit_007 Oct 19 '18 at 12:51
  • what is going inside recursive call that at each iteration the only int value from 0 to so on is going on WHAT is going inside the recursive call only int value is storing or returning How i get to know – surendraps007 Oct 19 '18 at 13:33
  • @surendraps007 just print the value of both variables before making any recursive call. You will see the changes. – suvojit_007 Oct 19 '18 at 13:38
  • https://ide.geeksforgeeks.org/dMr6cGKn5f This where the program we can see it is printing the 0 ,1, 2 etc But i want to know WHY only 1,2, .. is printing what is going inside the machine or code. PLEASE elaborate what is going inside i know the recursive call method – surendraps007 Oct 20 '18 at 15:52
0

maxDepth() is a recursive function which is called recursively until a NULL pointer is encountered indicating the end of the tree. maxDepth(node->left) is a recursive call to maxDepth function where the subtree is rooted at node->left. See the illustration below

           node
           /  \
       l_node   r_node
       / \      /\ 
    left  1    3  right
(lDepth)          (rDepth)

l_node=node->left so the maxDepth(l_node) is called. Similarly, r_node=node->right so the maxDepth(r_node) is called.

Hope this helps you.

Akhilesh Pandey
  • 868
  • 8
  • 18
  • i got it what you told ,But what the value like 1,2,3 or etc value is storing the lDepth variable, what is the type of value is strong in lDepth variable and HOW it is storing if it 1,2,3 ... – surendraps007 Oct 19 '18 at 12:47
  • @surendraps007 Each node is like a structure. Each node can store a value like 1,2,3 etc., a pointer to its left child, and a pointer to its right child something like this [left, data, right ]. To access the value use node->data, to access the left child used node->left. Similarly for the right child. – Akhilesh Pandey Oct 20 '18 at 09:59
  • https://ide.geeksforgeeks.org/dMr6cGKn5f This where the program we can see it is printing the 0 ,1, 2 etc But i want to know WHY only 1,2, .. is printing what is going inside the machine or code. PLEASE elaborate what is going inside i know the recursive call method – surendraps007 Oct 20 '18 at 15:55
  • Akhilesh Pandey please reply – surendraps007 Nov 02 '18 at 13:29
  • Sure brother... Let me have a look into your program. – Akhilesh Pandey Nov 03 '18 at 02:00