Interview question - find the next node of the binary tree

topic:

Given a binary tree and one other node, how to find the next node in the inorder traversal sequence? In addition to two pointers to the left and right nodes, the nodes in the tree also have a pointer to the parent node.

If the given sequence of this binary tree is {d, b, h, e, i, a, f, c, g}, analyze how to find the next node of the binary tree.

First of all, we can draw the picture of the binary tree according to the in-order traversal. Only when we draw the picture of the binary tree can we find its internal connection. So the picture is very important.

As shown below:
2327cd02ef034eef90dfa8b814afef75.png

 

We have to analyze different situations such as:

(Briefly explain, the right child node is the same as the right child, but the names are different.)

1. If a node has a right subtree, then its next node is the leftmost child node in its right subtree .

Explanation: Starting from the right child node and following the pointer to the left child node, you can find the next node.

Take node b as an example : in the in-order traversal, the next node of b is h, and in the binary tree graph, e is the right subtree of b, so find the left child of e. That is, h is the next node of b.

Taking a again as an example, the next node of a is the leftmost child of its right subtree, that is, f is the next node of a

2. If a node has no right subtree, if the node is the left node of its parent node , then its next node is its parent node.

Take node d as an example: d has no right subtree, and node d is the left child of its parent node, which satisfies the above conclusion, so its next node is b

In the same way, the next node of f is also c

3. If a node has neither right subtree, and it is also the right child node of its parent section ,

First:

We can traverse up the pointer to the parent node until we find a node that is the left child of its parent node . If such a node exists, then the parent node of this node is the next node we are looking for.

Take the i node as an example: the i node has no right subtree, and is the right child of its parent node. So we have to traverse upwards and find the e node, but the e node is not the left child of its parent node, so we continue to traverse upwards and find b, which is the left child of its parent node, so there is such a node, so this node The parent node of is the next node. That is, a is the next node of i.

second:

Similarly, we can traverse up along the pointer to the parent node , but we can't find a node that is the left child node of its parent node . So there is no.

Take node g as an example: g is the right child of its parent node, and there is no right subtree at the same time, so it meets the conditions. It has been traversing upwards to find node c, but c is the right child of its parent node, and then search upwards to find The a node is removed, and the a node has no negative nodes, so no node is found that is the left child node of its parent node, so the next node of g is not.

Code

BinaryTreeNode* GetNext(BinaryTreeNode* pNode)
{
	if (pNode == NULL)
	{
		return nullptr;
	}
	BinaryTreeNode* pNext = nullptr;
	if (pNode->m_right != nullptr)
	{
		BinaryTreeNode* pright = pNode->m_right
			while (pright->m_left != nullptr)
				pright = pright->m_left;
		pNext = pright;

	}
	else if(pNode->m_parent!=nullptr)
	{
		BinaryTreeNode* pcurrent = pNode;
		BinaryTreeNode* parent = pNode->m_parent;
		while (parent != nullptr && pcurrent == parent->m_right)
		{
			pcurrent = parent;
			parent = parent->m_parent;
		}
		pNext = parent;
	}
	return pNext;
}

 Notes:

BinaryTreeNode* pNext = nullptr;——Define a pointer to this node type.

while (pright->m_left != nullptr)——If it is not empty, the next node is the next left subtree of the node

pNext = pright;——If the left side is empty, the next node of the node is the right subtree of the node.

else if(pNode->m_parent!=nullptr) - the right child is null. But the parent node is not empty

 

Guess you like

Origin blog.csdn.net/2202_75623950/article/details/129913633