5.1.9—二叉树的遍历—Symmetric Tree
来源:互联网 发布:浪潮软件重大新闻 编辑:程序博客网 时间:2024/05/22 10:40
描述
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same
value.
Given two binary trees, write a function to check if they are equal or not.
Two binary trees are considered equal if they are structurally identical and the nodes have the same
value.
#include "BinaryTree.h"#include <stack>#include<queue>#include<vector>#include<algorithm>using namespace std;//===判断一棵二叉树是否对称------递归版本bool IsSymmetricTwoTree(BinaryTreeNode *proot1, BinaryTreeNode *proot2){if (!proot1&&!proot2) return true;else if (!proot1 || !proot2) return false;else{return (proot1->m_nValue == proot2->m_nValue)&& IsSymmetricTwoTree(proot1->m_pLeft, proot2->m_pRight)&& IsSymmetricTwoTree(proot1->m_pRight, proot2->m_pLeft);}}bool IsSymmetric1(BinaryTreeNode *proot){if (!proot) return true;else return IsSymmetricTwoTree(proot->m_pLeft, proot->m_pRight);}//===判断一棵二叉树是否对称------迭代版本bool IsSymmetric2(BinaryTreeNode *proot){if (!proot) return true;stack<BinaryTreeNode*> temp;temp.push(proot->m_pLeft);temp.push(proot->m_pRight);while (!temp.empty()){auto p= temp.top();temp.pop();auto q = temp.top();temp.pop();if (!p&&!q) continue;if (!p || !q) return false;if (p->m_nValue != q->m_nValue) return false;temp.push(q->m_pLeft);temp.push(p->m_pRight);temp.push(q->m_pRight);temp.push(p->m_pLeft);}return true;}// ====================测试代码====================// 8// 6 6// 9 9 9 9int main(){//===BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(5);ConnectTreeNodes(pNode8, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);//===//PrintTree(pNode8);//===bool flag1 = IsSymmetric1(pNode8);cout << flag1 << endl;//===bool flag2 = IsSymmetric2(pNode8);cout << flag2 << endl;DestroyTree(pNode8);}
阅读全文
0 0
- 5.1.9—二叉树的遍历—Symmetric Tree
- [leetcode] Symmetric Tree--二叉树遍历的应用
- 二叉树遍历变种101. Symmetric Tree
- leetcode 101.Symmetric Tree-对称二叉树|深度遍历
- Leetcode-Symmetric Tree——判断二叉树是否对称
- 5.1.8—二叉树的遍历—Same Tree
- 二叉树镜像,对称二叉树的判断-symmetric Tree
- [LeetCode][二叉树]Symmetric Tree
- 【LeetCode】101. Symmetric Tree 中序遍历,分支遍历,二叉树
- 判断二叉树是否是水平对称的 Symmetric Tree
- LeetCode(Symmetric Tree)判断二叉树是否是对称的
- 5.1.9 Symmetric Tree
- leetcode 101. Symmetric Tree-对称二叉数|深度遍历
- LeetCode101—Symmetric Tree
- 【二叉树是否对称】Symmetric Tree
- 【leetcode 二叉树对称判断】Symmetric Tree
- Symmetric Tree[LeetCode]对称二叉树
- Leetcode Symmetric Tree 对称二叉树
- 5.1.7—二叉树的遍历—Recover Binary Sear Tree
- spring mav创建和注销session
- 5.1.8—二叉树的遍历—Same Tree
- ural1143 Electric Path (区间dp)
- 在APP中播放音频文件
- 5.1.9—二叉树的遍历—Symmetric Tree
- HTTPS 服务搭建相关概念
- 卷积神经网络
- 5.1.10—二叉树的遍历—Balanced Binary Tree
- Java内存回收与分配
- 5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
- ?
- ConcurrentHashMap
- springmvc笔记-7-文件上传下载,拦截器