创建二叉树 创建单链表

来源:互联网 发布:windows10怎么设置网络 编辑:程序博客网 时间:2024/05/16 14:53
struct TreeNode{    int val;    TreeNode *left;    TreeNode *right;    TreeNode(int x) : val(x), left(NULL), right(NULL) {}}; TreeNode* createTreeNode(){        int d;        cin>>d;        TreeNode* r=NULL;        if(d!=0){            r=new TreeNode(d);            r->left=createTreeNode();            r->right=createTreeNode();        }        return r;    }    void show(TreeNode* p)//preorder output    {        if(p)        {            cout<<p->val<<",";            show(p->left);            show(p->right);        }        else        {            cout<<"#,";        }    }
struct ListNode {      int val;      ListNode *next;      ListNode(int x) : val(x), next(NULL) {}};    ListNode* createListNode(){        int d;        ListNode* head=NULL;        cin>>d;        if(d!=0){            head=new ListNode(d);            head->next=createListNode();        }        return head;    }    void show(ListNode* node){        ListNode* t=node;        while(t!=NULL){            cout<<t->val<<",";            t=t->next;        }        cout<<endl;    }



0 0
原创粉丝点击