二叉树镜像 -- 递归法

来源:互联网 发布:怎么在淘宝网开网店 编辑:程序博客网 时间:2024/06/18 09:57
#include <cstdio>  #include <iostream> #include <vector>  #include <set>#include <algorithm>#include <stack>#include <string>#include <queue>#include <unordered_map>#include <iterator>using namespace std;#define N 100int n , m;typedef struct node{    int data;    struct node* left;    struct node* right;    node(int _data = -1)    {        data = _data;        left = NULL;        right = NULL;    }}Bnode;//递归镜像 void bmirror(Bnode* root){    if(root == NULL)        return ;    Bnode* left = root->left ;    Bnode* right = root->right;    bmirror(left);    bmirror(right);    root->left = right; // 左右子树交换    root->right = left;}int main()  {      Bnode* b1 = new node(1);    Bnode* b2 = new node(2);    Bnode* b3 = new node(3);    Bnode* b4 = new node(4);    Bnode* b5 = new node(5);    Bnode* b6 = new node(6);    b1->left = b2;    b1->right = b3;    b2->left = b4;    b2->right = b5;    b3->left = b6;    /*              1                        1            /  \                     /   \          2     3                   3     2          / \    /       镜像         \    / \        4   5  6                      6  5   4    */    bmirror(b1);    return 0;  } 
0 0
原创粉丝点击