## Q. Program to find the largest element in a Binary Tree.

### Explanation

In this program, we will find out the largest node in the given binary tree. We first define variable max that will hold root's data. Then, we traverse through the left sub-tree to find the largest node. Compare it with max and store the maximum of two in a variable max. Then, we traverse through the right subtree to find the largest node and compare it with max. In the end, max will have the largest node. Above diagram represents a binary tree. Initially, max will hold 15. Recursive through left subtree.

Recursive through right subtree.

Recurse in the left subtree of 35

Recurse in the right subtree of 35

So, the largest node in above binary tree is 74.

### Algorithm

1. Define the class Node which has three attributes namely: data, left, and right. Here, left represents the left child of the node and right represents the right child of the node.
2. Assign the data part of the node with an appropriate value and assign left and right to null.
3. Define another class which has an attribute root.
1. Root represents the root node of the tree which is initialized to null.
2. largestElement() will find out the largest node in the binary tree:
1. It checks whether the root is null, which means the tree is empty.
2. If the tree is not empty, define a variable max that will store temp's data.
3. Find out the maximum node in the left subtree by calling largestElement() recursively. Store that value in leftMax. Compare the value of max with leftMax and store the maximum of two to max.
4. Find out the maximum node in right subtree by calling largestElement() recursively. Store that value in rightMax. Compare the value of max with rightMax and store the maximum of two to max.
5. In the end, max will hold the largest node in the binary tree.

### Python

Output:

```Largest element in the binary tree: 74
```

### C

Output:

```Largest element in the binary tree: 74
```

### JAVA

Output:

```Largest element in the binary tree: 74
```

### C#

Output:

```Largest element in the binary tree: 74
```

### PHP

Output:

```Largest element in the binary tree: 74
```

Next TopicPrograms List

### Feedback   