剑指offer之二叉树镜像
来源:互联网 发布:32位 windows 内存支持 编辑:程序博客网 时间:2024/06/08 11:46
1.二叉树镜像是指左右对换一个二叉树
1 / \ 2 4 / / 3 5
变成
1 / \ 4 2 \ \ 5 3
递归交换非叶子节点左右子树即可
2.代码
#include<stdio.h>#include<stack>struct BinaryTree{ int value; BinaryTree* left; BinaryTree* right;};void mirrorTreeDFS(BinaryTree* root){ if(root==NULL || (root->left==NULL && root->right==NULL)) return; BinaryTree *tmp = root->left; root->left = root->right; root->right = tmp; if(root->left) mirrorTreeDFS(root->left); if(root->right) mirrorTreeDFS(root->right);}void mirrorTree(BinaryTree* root){ if(root==NULL || (root->left==NULL && root->right==NULL)) return; std::stack nodeStack; nodeStack.push(root); while(nodeStack.size() > 0) { BinaryTree* cur = nodeStack.top(); nodeStack.pop(); BinaryTree* tmp = cur->left; cur->left = cur->right; cur->right = tmp; if(cur->left) nodeStack.push(cur->left); if(cur->right) nodeStack.push(cur->right); }}
0 0
- 剑指offer之二叉树镜像
- 剑指offer-二叉树镜像
- 剑指offer 二叉树镜像
- 剑指offer:二叉树镜像
- 《剑指offer》二叉树镜像
- 剑指Offer之二叉树重构
- 来自《剑指offer》 二叉树镜像
- 剑指Offer 19 : 二叉树镜像
- 剑指Offer之二叉树的深度
- 剑指OFFER之二叉树篇
- 《剑指offer》学习之--二叉树
- 剑指offer之重建二叉树
- 剑指offer之二叉树的深度
- 剑指Offer之 - 二叉树的深度
- 剑指Offer之 - 重建二叉树
- 剑指offer之重建二叉树
- 剑指offer之重建二叉树
- 剑指offer之5 重建二叉树
- 使用Babel和Broserify创建你的ES6项目
- Geany无法切换中文输入法
- 【Flask】Request的中断和ErrorHandler
- hdu 1255 线段树+离散化+扫描线 (矩形面积交
- 速学Promise传递异步消息初步
- 剑指offer之二叉树镜像
- 二叉搜索树的后序遍历序列
- Java四种线程池的使用
- MyEclipse字体和保护色标准个人设置
- 第三章 3.20
- python多线程异常处理
- Mac与Linux之间网络配置
- 蚂蚁爬行问题及其改编
- Redis 实战小计