Binary Tree Image

Source: Internet
Author: User


Naturally, recursion:

Code:

# Include <iostream> # include <list> using namespace STD; typedef struct tree1 {int data; struct tree1 * lchild; struct tree1 * rchild;} tree, * ptree; void createtree (ptree & P) {int temp; scanf ("% d", & temp); If (temp! = 0) {P = (ptree) malloc (sizeof (tree); P-> DATA = temp; createtree (p-> lchild ); createtree (p-> rchild);} else {P = NULL;} void print (ptree p) {If (P) {cout <p-> data <"; print (p-> lchild); print (p-> rchild) ;}elsereturn ;}void implements rever (ptree & P) {// if it is an empty tree or only one node if (null = p | null = p-> lchild & null = p-> rchild) {return ;} ptree temp = p-> lchild; P-> lchild = p-> rchild; P-> rchild = temp; If (p-> lchild! = NULL) returns rever (p-> lchild); If (p-> rchild! = NULL) repeated rever (p-> rchild);} int main () {ptree phead = NULL; createtree (phead); cout <Endl; print (phead ); cout <Endl <"flip" <Endl; reverse rever (phead); print (phead); Return 0 ;}

Running result:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.