求二叉树中序遍历的第一个节点

#include <iostream> #include<bits/stdc++.h> using namespace std; typedef struct node{ int data; struct node *lchild,*rchild; }treenode; int cretree(treenode* &root){

int x; cin>>x;

if(x==0) root=NULL; else{ root=new treenode; root->data=x; cretree(root->lchild); cretree(root->rchild); } return 0;

}

int visit(treenode *root){ if(root==NULL) return 0;

visit(root->lchild); cout<<root->data<<" "; visit(root->rchild); return 0; }

核心代码       其他为创建二叉树,中序遍历等配件函数
void f(treenode *root){
treenode *p;
p=root;
while(p->lchild) p=p->lchild;
cout<<p->data<<endl;

} int main() { treenode *root;

cretree(root); visit(root); cout<<endl; f(root);

// cout << "Hello world!" << endl; return 0; }//1 2 3 0 0 4 5 0 6 0 0 7 0 0 0

经验分享 程序员 微信小程序 职场和发展