题干
C++实现
循环+双指针法(一个指向父亲,一个指向待插入结点)
# define _CRT_SECURE_NO_WARNINGS
# include <stdio.h>
# include <queue>
using namespace std;
struct TreeNode {
char data;
TreeNode* left;
TreeNode* right;
} ;
void InsertBST ( TreeNode* & proot, int data) {
TreeNode* pNew = new TreeNode ( ) ;
pNew-> data = data;
pNew-> left = NULL ;
pNew-> right = NULL ;
if ( proot == NULL ) {
proot = pNew;
printf ( "-1\n" ) ;
}
else {
TreeNode* pCur = proot;
TreeNode* pPre = NULL ;
while ( 1 ) {
if ( pCur-> data > data) {
pPre = pCur;
pCur = pCur-> left;
if ( pCur == NULL ) {
pPre-> left = pNew;
printf ( "%d\n" , pPre-> data) ;
break ;
}
}
else {
pPre = pCur;
pCur = pCur-> right;
if ( pCur == NULL ) {
pPre-> right = pNew;
printf ( "%d\n" , pPre-> data) ;
break ;
}
}
}
}
}
int main ( ) {
int n;
scanf ( "%d" , & n) ;
TreeNode* proot = NULL ;
for ( int i = 0 ; i < n; i++ )
{
int data;
scanf ( "%d" , & data) ;
InsertBST ( proot, data) ;
}
return 0 ;
}