5.1.8—二叉树的遍历—Same Tree
来源:互联网 发布:浪潮软件重大新闻 编辑:程序博客网 时间:2024/06/06 09:37
描述
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 IsSameTwpTree(BinaryTreeNode *proot1, BinaryTreeNode *proot2){if (proot1&&proot2){if (proot1->m_nValue == proot2->m_nValue){return (IsSameTwpTree(proot1->m_pLeft, proot2->m_pLeft) && IsSameTwpTree(proot1->m_pRight, proot2->m_pRight));}else{return false;}}else if ((!proot1&&proot2) || (proot1&&!proot2)){return false;}else{return true;}}// ====================测试代码====================// 8// 6 10// 5 7 9 11int main(){//===BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);{BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(11);ConnectTreeNodes(pNode8, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);}//===BinaryTreeNode* pNode8_1 = CreateBinaryTreeNode(8);{BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);BinaryTreeNode* pNode7 = CreateBinaryTreeNode(7);BinaryTreeNode* pNode9 = CreateBinaryTreeNode(9);BinaryTreeNode* pNode11 = CreateBinaryTreeNode(12);ConnectTreeNodes(pNode8_1, pNode6, pNode10);ConnectTreeNodes(pNode6, pNode5, pNode7);ConnectTreeNodes(pNode10, pNode9, pNode11);}//===//PrintTree(pNode8);//PrintTree(pNode8_1);//===bool flag = IsSameTwpTree(pNode8, pNode8_1);cout << flag << endl;DestroyTree(pNode8);DestroyTree(pNode8_1);}
阅读全文
0 0
- 5.1.8—二叉树的遍历—Same Tree
- 5.1.9—二叉树的遍历—Symmetric Tree
- C++二叉树遍历总结\100. Same Tree
- leetcode 100. Same Tree 二叉树DFS深度优先遍历
- [LeetCode][二叉树]Same Tree
- 二叉树系列---same-tree
- LeetCode | Same Tree(相等的二叉树)
- 5.1.1—二叉树的遍历—Binary Tree Preorder Traversal
- 5.1.2—二叉树的遍历—Binary Tree Inorder Traversal
- 5.1.3—二叉树的遍历—Binary Tree Postorder Traversal
- 5.1.4—二叉树的遍历—Binary Tree Level Order Traversal
- 5.1.5—二叉树的遍历—Binary Tree Level Order Traversal II
- 5.1.6—二叉树的遍历—Binary Tree Zigzag Level Order Traversal
- 5.1.7—二叉树的遍历—Recover Binary Sear Tree
- 5.1.10—二叉树的遍历—Balanced Binary Tree
- 5.1.11—二叉树的遍历—Flatten Binary Tree to Linked List
- 5.1.8 Same Tree
- Binary Tree——给定二叉树的前序遍历和中序遍历重建二叉树
- 5.1.6—二叉树的遍历—Binary Tree Zigzag Level Order Traversal
- Java 静态绑定与动态绑定
- nb物联网 bc95模块联网
- 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