Print Deepest node

How should i print the deepest node,i using count to print but it doesn't work pls help and guide me.Here are the code i have write.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  template <class T>
bool BST<T>::deepestNode()
{
	BST<T> *cur;
	int	counter = 0;
	if (root == NULL) return false;
	Deepest2(root, counter);
}

template <class T>
bool BST<T>::Deepest2(BTNode<T> *cur, int &count) {
	if (cur == NULL) return false;
	if (cur != NULL){
		Deepest2(cur->left, count);
		Deepest2(cur->right, count);
		count++;
	}
	if (count == cur->item.id)
	{
		cout << cur->item.id << " " << endl;
		return true;
	}
}
What does "deepest" mean? What if many are equally "deep"?

Do the nodes store their depth?
Yes the node store their depth. The binary in my program have these number(28,125,22,23,21,19). The deepest mean print the deepest level which is 21 only.
I have found another way to print the deepest node, but it print like this:
21
19
23
22
125
28
I want to print 21 only, how should i do?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
template <class T>
bool BST<T>::deepestNode()
{
	BTNode<T> *cur;
	cur = root;
	if (root == NULL)return false;
	Deepest2(cur, 0);
	
}

template <class T>
bool BST<T>::Deepest2(BTNode<T> *cur, int count) {
	if (cur == NULL) return false;
	if (cur != NULL){
		Deepest2(cur->left, ++count);
		if (count > deepestlevel){
			value == cur->item.id;
			deepestlevel = count;
		}
		Deepest2(cur->right, count);

	}
	cout << cur->item.id << " " << endl;
}
the node store their depth

Then perhaps something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
U * foo( U * node ) {
  if ( ! node ) return nullptr;

  auto left  = foo( node->left );
  auto right = foo( node->rigth );
  if ( left && right ) {
    return ( left->depth < right->depth ) ? right : left;
  }
  else if ( left ) return left;
  else if ( right ) return right;
  else return node;
}

template <class T>
bool BST<T>::deepestNode()
{
  auto deep = foo( root );
  if ( deep ) std::cout << *deep;
  return deep;
}

Topic archived. No new replies allowed.