// 面试题8:二叉树的下一个结点 |
| // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? |
| // 树中的结点除了有两个分别指向左右子结点的指针以外,还有一个指向父结点的指针。 |
#include<iostream>
using namespace std;
struct BinaryTreeNode{
int m_nValue;
BinaryTreeNode *m_pParent;
BinaryTreeNode *m_pLeft;
BinaryTreeNode *m_pRight;
};
BinaryTreeNode *CreateBinaryTreeNode(int value);
void ConnectTreeNodes(BinaryTreeNode* pParent, BinaryTreeNode* pLeft, BinaryTreeNode* pRight);
void printTree(BinaryTreeNode *root);
void PrintTreeNode(BinaryTreeNode* pNode);
BinaryTreeNode *GetNextNode(BinaryTreeNode* pNode);
int main(){
BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);
BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);
BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);
ConnectTreeNodes(pNode8, pNode6, pNode10);
ConnectTreeNodes(pNode6, pNode5, pNode7);
ConnectTreeNodes(pNode10, pNode9, pNode11);
printTree(pNode8);
//BinaryTreeNode* pNode = pNode8;
//BinaryTreeNode* pNode = pNode6;
//BinaryTreeNode* pNode = pNode5;
//BinaryTreeNode* pNode = pNode7;
//BinaryTreeNode* pNode = pNode10;
//BinaryTreeNode* pNode = pNode9;
BinaryTreeNode* pNode = pNode11;
BinaryTreeNode* nextNode = GetNextNode(pNode);
if(pNode == nullptr)
cout<<"没有下一个节点"<<endl;
else if(nextNode != nullptr)
cout<<endl<<pNode->m_nValue<<" 的下一个节点: "<<nextNode->m_nValue<<endl;
else
cout<<endl<<pNode->m_nValue<<" 没有下一个节点"<<endl;
}
//找到中序遍历序列中某节点的下一个节点
BinaryTreeNode *GetNextNode(BinaryTreeNode* pNode){
if(pNode == nullptr)
return nullptr;
if(pNode->m_pRight != nullptr){//有右子树,找到右子树第一个左节点
BinaryTreeNode *currNode = pNode->m_pRight;
while(currNode->m_pLeft != nullptr)
currNode = currNode->m_pLeft;
return currNode;
}
else{//没有右子树的情况下
if(pNode->m_pParent != nullptr){
BinaryTreeNode *parent = pNode->m_pParent;
if(parent->m_pLeft == pNode)//是父节点的左节点
return parent;//父节点是下一个子节点
else{//是父节点的右节点,向上找到一个祖先结点,使该祖先结点的左子节点是我们的父结点
if(parent->m_pParent != nullptr){
BinaryTreeNode *ancesNode = parent->m_pParent;
if(ancesNode->m_pLeft == parent)
return ancesNode;
}
}
}
}
return nullptr;
}
/*
BinaryTreeNode *GetNextNode(BinaryTreeNode* pNode){
if(pNode == nullptr)
return nullptr;
if(pNode->m_pRight != nullptr){//有右子树,找到右子树第一个左节点
BinaryTreeNode *currNode = pNode->m_pRight;
while(currNode->m_pLeft != nullptr)
currNode = currNode->m_pLeft;
return currNode;
}
else{//没有右子树的情况下
if(pNode->m_pParent != nullptr){
BinaryTreeNode *current = pNode;
BinaryTreeNode *parent = pNode->m_pParent;
//向上找到第一个父亲结点,使得该节点是父亲结点的左子结点
while(parent != nullptr && parent->m_pRight == current){
current = parent;
parent = parent->m_pParent;
}
return parent;
}
}
return nullptr;
}
*/
BinaryTreeNode *CreateBinaryTreeNode(int value){
BinaryTreeNode *pNode = new BinaryTreeNode();
pNode->m_nValue = value;
pNode->m_pParent = nullptr;
pNode->m_pLeft = nullptr;
pNode->m_pRight = nullptr;
return pNode;
}
void ConnectTreeNodes(BinaryTreeNode* pParent,BinaryTreeNode *pLeft,BinaryTreeNode *pRight){
if(pParent != nullptr){
if(pLeft != nullptr)
pLeft->m_pParent = pParent;
if(pRight != nullptr)
pRight->m_pParent = pParent;
pParent->m_pLeft = pLeft;
pParent->m_pRight = pRight;
}
}
void PrintTreeNode(BinaryTreeNode* pNode)
{
if(pNode != nullptr)
{
cout<<"value of this node is: "<<pNode->m_nValue<<endl;
if(pNode->m_pLeft != nullptr)
cout<<"value of its left child is: "<<pNode->m_pLeft->m_nValue<<endl;
else
cout<<"left child is null"<<endl;
if(pNode->m_pRight != nullptr)
cout<<"value of its right child is: "<<pNode->m_pRight->m_nValue<<endl;
else
cout<<"right child is null."<<endl;
}
else
{
cout<<"this node is null"<<endl;
}
}
void printTree(BinaryTreeNode *root){
if(root == nullptr)
return;
PrintTreeNode(root);
printTree(root->m_pLeft);
printTree(root->m_pRight);
}