how to compare the biggest node with the same level BST node c++

// C++ program to find largest
// value on each level of binary tree.
#include <bits/stdc++.h>
using namespace std;

/* A binary tree node has data,
pointer to left child and a
pointer to right child */
struct Node {
	int val;
	struct Node *left, *right;
};

/* Recursive function to find
the largest value on each level */
void helper(vector<int>& res, Node* root, int d)
{
	if (!root)
		return;

	// Expand list size
	if (d == res.size())
		res.push_back(root->val);

	else

		// to ensure largest value
		// on level is being stored
		res[d] = max(res[d], root->val);

	// Recursively traverse left and
	// right subtrees in order to find
	// out the largest value on each level
	helper(res, root->left, d + 1);
	helper(res, root->right, d + 1);
}

// function to find largest values
vector<int> largestValues(Node* root)
{
	vector<int> res;
	helper(res, root, 0);
	return res;
}

/* Helper function that allocates a
new node with the given data and
NULL left and right pointers. */
Node* newNode(int data)
{
	Node* temp = new Node;
	temp->val = data;
	temp->left = temp->right = NULL;
	return temp;
}

// Driver code
int main()
{
	/* Let us construct a Binary Tree
		4
	/ \
	9 2
	/ \ \
	3 5 7 */

	Node* root = NULL;
	root = newNode(4);
	root->left = newNode(9);
	root->right = newNode(2);
	root->left->left = newNode(3);
	root->left->right = newNode(5);
	root->right->right = newNode(7);
	
	vector<int> res = largestValues(root);
	for (int i = 0; i < res.size(); i++)
		cout << res[i] << " ";
		
	return 0;
}

3.78
9
Awgiedawgie 440220 points

                                    // C++ implementation to print largest
// value in each level of Binary Tree
#include &lt;bits/stdc++.h&gt;

using namespace std;

// structure of a node of binary tree
struct Node {
	int data;
	Node *left, *right;
};

// function to get a new node
Node* newNode(int data)
{
	// allocate space
	Node* temp = new Node;

	// put in the data
	temp-&gt;data = data;
	temp-&gt;left = temp-&gt;right = NULL;
	return temp;
}

// function to print largest value
// in each level of Binary Tree
void largestValueInEachLevel(Node* root)
{
	// if tree is empty
	if (!root)
		return;

	queue&lt;Node*&gt; q;
	int nc, max;

	// push root to the queue 'q'
	q.push(root);

	while (1) {
		// node count for the current level
		nc = q.size();

		// if true then all the nodes of
		// the tree have been traversed
		if (nc == 0)
			break;

		// maximum element for the current
		// level
		max = INT_MIN;

		while (nc--) {

			// get the front element from 'q'
			Node* front = q.front();

			// remove front element from 'q'
			q.pop();

			// if true, then update 'max'
			if (max &lt; front-&gt;data)
				max = front-&gt;data;

			// if left child exists
			if (front-&gt;left)
				q.push(front-&gt;left);

			// if right child exists
			if (front-&gt;right)
				q.push(front-&gt;right);
		}

		// print maximum element of
		// current level
		cout &lt;&lt; max &lt;&lt; &quot; &quot;;
	}
}

// Driver code
int main()
{
	/* Construct a Binary Tree
		4
	/ \
	9 2
	/ \ \
	3 5 7 */

	Node* root = NULL;
	root = newNode(4);
	root-&gt;left = newNode(9);
	root-&gt;right = newNode(2);
	root-&gt;left-&gt;left = newNode(3);
	root-&gt;left-&gt;right = newNode(5);
	root-&gt;right-&gt;right = newNode(7);

	// Function call
	largestValueInEachLevel(root);

	return 0;
}

3.78 (9 Votes)
0
0
0
Awgiedawgie 440220 points

                                    // C++ implementation to print largest
// value in each level of Binary Tree
#include &lt;bits/stdc++.h&gt;

using namespace std;

// structure of a node of binary tree
struct Node {
	int data;
	Node *left, *right;
};

// function to get a new node
Node* newNode(int data)
{
	// allocate space
	Node* temp = new Node;

	// put in the data
	temp-&gt;data = data;
	temp-&gt;left = temp-&gt;right = NULL;
	return temp;
}

// function to print largest value
// in each level of Binary Tree
void largestValueInEachLevel(Node* root)
{
	// if tree is empty
	if (!root)
		return;

	queue&lt;Node*&gt; q;
	int nc, max;

	// push root to the queue 'q'
	q.push(root);

	while (1) {
		// node count for the current level
		nc = q.size();

		// if true then all the nodes of
		// the tree have been traversed
		if (nc == 0)
			break;

		// maximum element for the current
		// level
		max = INT_MIN;

		while (nc--) {

			// get the front element from 'q'
			Node* front = q.front();

			// remove front element from 'q'
			q.pop();

			// if true, then update 'max'
			if (max &lt; front-&gt;data)
				max = front-&gt;data;

			// if left child exists
			if (front-&gt;left)
				q.push(front-&gt;left);

			// if right child exists
			if (front-&gt;right)
				q.push(front-&gt;right);
		}

		// print maximum element of
		// current level
		cout &lt;&lt; max &lt;&lt; &quot; &quot;;
	}
}

// Driver code
int main()
{
	/* Construct a Binary Tree
		4
	/ \
	9 2
	/ \ \
	3 5 7 */

	Node* root = NULL;
	root = newNode(4);
	root-&gt;left = newNode(9);
	root-&gt;right = newNode(2);
	root-&gt;left-&gt;left = newNode(3);
	root-&gt;left-&gt;right = newNode(5);
	root-&gt;right-&gt;right = newNode(7);

	// Function call
	largestValueInEachLevel(root);

	return 0;
}

0
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source