设为首页 加入收藏

TOP

C++算法之 判断是否为平衡二叉树 求二叉树的镜像(二)
2015-07-20 17:25:12 来源: 作者: 【 】 浏览:7
Tags:算法 判断 是否 平衡
nced3(pRoot, depth); } /* 求二叉树的镜像: 方法1: 前序遍历每个节点,如果遍历到的节点有子节点,就交换它的两个子节点。(先交换左子树和右子树,再对左子树和右子树进行镜像操作) 方法2: 如果二叉树不为空,求左子树和右子树的镜像,然后再交换左子树和右子树 */ void Mirror(BTree* &pRoot) { if(pRoot == NULL) return; if(pRoot->m_pLeft ==NULL && pRoot->m_pRight == NULL) return; BTree* pTemp = pRoot->m_pLeft; pRoot->m_pLeft = pRoot->m_pRight; pRoot->m_pRight = pTemp; if(pRoot->m_pLeft) Mirror(pRoot->m_pLeft); if(pRoot->m_pRight) Mirror(pRoot->m_pRight); } BTree* Mirror2(BTree* pRoot) { if(pRoot == NULL) return NULL; BTree* pLeft = Mirror2(pRoot->m_pLeft); BTree* pRight = Mirror2(pRoot->m_pRight); pRoot->m_pLeft = pRight; pRoot->m_pRight = pLeft; return pRoot; } void PrintPrev(BTree* pRoot) { if(pRoot == NULL) return; cout< m_nValue<<" "; PrintPrev(pRoot->m_pLeft); PrintPrev(pRoot->m_pRight); } int _tmain(int argc, _TCHAR* argv[]) { BTree* m_pRoot = new BTree(9); Insert(3,m_pRoot); Insert(6,m_pRoot); Insert(1,m_pRoot); Insert(2,m_pRoot); Insert(5,m_pRoot); Insert(8,m_pRoot); Insert(7,m_pRoot); Insert(10,m_pRoot); cout<


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇NYOJ 972 核桃的数量 (最小公倍数) 下一篇归并排序(MergeSort)

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·求navicat for mysql (2025-12-26 13:21:33)
·有哪位大哥推荐一下m (2025-12-26 13:21:30)
·MySQL下载与安装教程 (2025-12-26 13:21:26)
·Linux_百度百科 (2025-12-26 12:51:52)
·Shell 流程控制 | 菜 (2025-12-26 12:51:49)