# Title:

Given a binary tree and one of its nodes, please find the next node in the middle order traversal order and return. Note that the nodes in the tree contain not only left and right child nodes, but also pointers to parent nodes.

# Problem solving ideas:

Let's take the above figure as an example. The middle order traversal of the binary tree in the figure above is d,b,h,e,i,a,f,c,g. We take this tree as an example to analyze how to find the next node of the binary tree.

Finding the next node according to a given node can be divided into two cases: the node has a right subtree and no right subtree:

If a node has a right subtree, its next node is the leftmost child of its right subtree. That is, starting from the right child node and following the pointer to the left child tree node, we can find its next node. For example, the next node of node b in the figure is h, and the next node of node a is f.

Next, we analyze the case where the node has no right subtree. If a node is the left child of its parent node, its next node is its parent node. For example, the next node of node d in the figure is b, and the next node of f is c.

If a node has no right subtree, and it is also the right child of the parent node, this situation is more complex. 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, the parent node of this node is the next node we are looking for. For example, in order to find the next node of node g, we traverse up the pointer to the parent node and reach node c first. Since node c is the right node of parent node a, we continue to traverse up to node a. Because node a is the root node of the tree. It has no parent node. Therefore, node G has no next node.

# C + + implementation

#include<iostream> using namespace std; //Binary tree node with parent pointer class BinaryTreeNode1 { public: int m_nvalue; BinaryTreeNode1* m_pLeft; BinaryTreeNode1* m_pRight; BinaryTreeNode1* m_pParent; BinaryTreeNode1(int x) :m_nvalue(x), m_pLeft(NULL), m_pRight(NULL), m_pParent(NULL){} // The initialization value of the current node is x, and the left and right subtrees and parent nodes are empty }; // Find the next node according to the given node class NextNodeBinaryTrees { public: BinaryTreeNode1 *GetNext(BinaryTreeNode1 *pNode) { if (pNode == nullptr) return NULL; BinaryTreeNode1 *pNext = nullptr; if (pNode->m_pRight!=nullptr) { while (pNode->m_pRight->m_pLeft!=nullptr) { pNode->m_pRight = pNode->m_pRight->m_pLeft; } pNext = pNode->m_pRight; } else if (pNode->m_pParent != nullptr) { BinaryTreeNode1 *pCurrent = pNode; BinaryTreeNode1 *pParent = pNode->m_pParent; while (pParent!=nullptr&&pCurrent==pParent->m_pRight) { pCurrent = pParent; pParent = pParent->m_pParent; } if (pParent != nullptr) { pNext = pParent; } else { pNext = NULL; } } return pNext; } }; //test case int main(){ BinaryTreeNode1 *root =new BinaryTreeNode1(1); root->m_pLeft= new BinaryTreeNode1(2); root->m_pLeft->m_pParent = root; root->m_pRight = new BinaryTreeNode1(3); root->m_pRight->m_pParent = root; root->m_pLeft->m_pLeft = new BinaryTreeNode1(4); root->m_pLeft->m_pLeft->m_pParent = root->m_pLeft; root->m_pLeft->m_pRight = new BinaryTreeNode1(5); root->m_pLeft->m_pRight->m_pParent = root->m_pLeft; NextNodeBinaryTrees np; cout<<np.GetNext(root->m_pLeft->m_pLeft)->m_nvalue<<endl; //Find the next node of 4 cout << np.GetNext(root->m_pRight)->m_nvalue << endl; //Find the next node of 3 cout << np.GetNext(root->m_pLeft)->m_nvalue <<endl; //Find the next node of 2 system("pause"); return 0; }

# test case

- Ordinary binary tree
- Special binary tree
- The next node of a node at a different location