博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
16--二叉树的镜像
阅读量:6660 次
发布时间:2019-06-25

本文共 2582 字,大约阅读时间需要 8 分钟。

////  main.cpp//  subStructureInTree////  Created by Hugo Cao  on 15/7/10.//  Copyright (c) 2015年 Hugo Cao . All rights reserved.///* 问题描述:    二叉树的镜像:        围绕着中心轴做左右调换。就这么短短多几行代码,做递归就可以了。void mirrorRecuresiveriy(binTree bt){    //判断是否为空,或者是子节点为空。    if (bt == NULL || (bt->m_pleft == NULL && bt->m_pright == NULL))    {        return ;    }    //交换左右子树的位置    binTree pTemp = bt->m_pleft;    bt->m_pleft = bt->m_pright;    bt->m_pright = pTemp;    //传入左结点。    if (bt->m_pleft)        mirrorRecuresiveriy(bt->m_pleft);    //传入右结点。    if (bt->m_pright)        mirrorRecuresiveriy(bt->m_pright);}  */#include 
using namespace std;#define Data inttypedef struct binaaryTreeNode{ Data m_value; binaaryTreeNode *m_pleft; binaaryTreeNode *m_pright;}btNode, *binTree;binTree addNode(Data value){ binTree bt = NULL; bt = new btNode; if (bt == NULL) { cout << "申请地址错误" << endl; return NULL; } bt->m_value = value; bt->m_pleft = NULL; bt->m_pright = NULL; return bt;}bool connectTreeNode(binTree parrentNode, binTree leftSonNode = NULL, binTree rightSonNode = NULL){ if (parrentNode == NULL || (leftSonNode == NULL && rightSonNode == NULL)) { cout << "结点输入问题" << endl; return false; } parrentNode->m_pleft = leftSonNode; parrentNode->m_pright = rightSonNode; return true;}binTree createBinaryATree(binTree Ahead){ binTree p1 = addNode(8); binTree p2 = addNode(8); binTree p3 = addNode(7); binTree p4 = addNode(9); binTree p5 = addNode(2); binTree p6 = addNode(4); binTree p7 = addNode(7); connectTreeNode(p1, p2, p3); connectTreeNode(p2, p4, p5); connectTreeNode(p5, p6, p7); return (Ahead = p1);}//树的镜像。void mirrorRecuresiveriy(binTree bt){ //判断是否为空,或者是子节点为空。 if (bt == NULL || (bt->m_pleft == NULL && bt->m_pright == NULL)) { return ; } //交换左右子树的位置 binTree pTemp = bt->m_pleft; bt->m_pleft = bt->m_pright; bt->m_pright = pTemp; //传入左结点。 if (bt->m_pleft) mirrorRecuresiveriy(bt->m_pleft); if (bt->m_pright) mirrorRecuresiveriy(bt->m_pright);}void RLR_print_BinaryTree(binTree bt){ if (bt == NULL) return ; cout << bt->m_value << " " << endl; RLR_print_BinaryTree(bt->m_pleft); RLR_print_BinaryTree(bt->m_pright);}int main(){ binTree bt_A = NULL; bt_A = createBinaryATree(bt_A); RLR_print_BinaryTree(bt_A); cout << endl; mirrorRecuresiveriy(bt_A); RLR_print_BinaryTree(bt_A); return 0;}

 

转载于:https://www.cnblogs.com/hgonlywj/p/4842564.html

你可能感兴趣的文章
2013-7-11学习作业[有面试题]
查看>>
appstore审核因 ipv6 被拒的问题
查看>>
AD RMS服务客户端故障排错
查看>>
深入浅出Hyper-V网络虚拟化(序)
查看>>
css样式表中的样式的优先级
查看>>
cocos2dx android mk解释
查看>>
在Mac OS X El Capitan上安装PHP
查看>>
ThinkPad 安装win7 64 位系统不能识别联想的外置DVD
查看>>
std::unique and removing duplicates from a container of objects
查看>>
JSON-RPC请求
查看>>
MMSegAnalyzer 自定义 同义词分词器
查看>>
Spark Core Runtime分析: DAGScheduler, TaskScheduler, SchedulerBackend
查看>>
我的友情链接
查看>>
什么是CGI
查看>>
grep命令及正则表达式的使用和介绍
查看>>
linux tr替代文件内容
查看>>
CentOs7.3 编译安装 Nginx 1.9.9
查看>>
java排序之快速排序、归并排序、基数排序
查看>>
一个故事讲完进程、线程和协程
查看>>
RedHat7 开放端口号
查看>>