# Find the node with the largest data in Binary Tree in Python

Here in this tutorial, we will learn how to find the node with the largest data in Binary Tree in Python with the help of recursion.

As we know Binary Trees are data structures with a maximum of two child nodes. And these nodes are represented as the left node and right node.

To learn how Binary Trees are created and implemented you can refer to:

How to implement Binary Tree in Python

## Finding the node with the largest data

def largestData(root): if root == None: return -1 largestLeftNode = largestData(root.left) largestRightNode = largestData(root.right) finalLargest = max(largestLeftNode, largestRightNode, root.data) return finalLargest

Now let us understand it with an example:

In the code written above and in the image displayed, we have taken a function called `largestData()`

to find the largest element in the Binary Tree.

we will return ‘-1’ if the root node is empty that is our base case.

Then we will call recursion on the left and the right node of the Binary Tree respectively and store the value in the variable called ‘largestLeftNode’ and ‘largestRightNode’.

Finally, we will compare the three stored values ‘`largestLeftNode`

‘, ‘`largestRightNode`

‘, and ‘`root.data`

‘ to find the maximum element.

In the end, we will return the largest data found among all the nodes.

Thus we have learned how to find the node with the largest data.

## Leave a Reply