【遍历二叉树】08判断两个二叉树是否相同【Same Tree】
迭代版本用的是二叉树的DFS,中的root->right->left
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
给定两个二叉树,写一个函数判断他们是否是相同的。
如果两个二叉树的结构相同而且每个节点里面的值也相同,那么认为他们是相同的二叉树。
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
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.
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
test.cpp:
递归版本:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ bool isSameTree(TreeNode *p, TreeNode *q) { //终止条件 if (!p && !q) { return true; } //剪枝 if (!p || !q) { return false; } //三方合并 return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); } // 树中结点含有分叉, // 6 // / \ // 7 2 // / \ // 1 4 // / \ // 3 5 int main() { TreeNode *pNodeA1 = CreateBinaryTreeNode(6); TreeNode *pNodeA2 = CreateBinaryTreeNode(7); TreeNode *pNodeA3 = CreateBinaryTreeNode(2); TreeNode *pNodeA4 = CreateBinaryTreeNode(1); TreeNode *pNodeA5 = CreateBinaryTreeNode(4); TreeNode *pNodeA6 = CreateBinaryTreeNode(3); TreeNode *pNodeA7 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5); ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7); TreeNode *pNodeB1 = CreateBinaryTreeNode(6); TreeNode *pNodeB2 = CreateBinaryTreeNode(7); TreeNode *pNodeB3 = CreateBinaryTreeNode(2); TreeNode *pNodeB4 = CreateBinaryTreeNode(1); TreeNode *pNodeB5 = CreateBinaryTreeNode(4); TreeNode *pNodeB6 = CreateBinaryTreeNode(3); TreeNode *pNodeB7 = CreateBinaryTreeNode(10); ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3); ConnectTreeNodes(pNodeB2, pNodeB4, pNodeB5); ConnectTreeNodes(pNodeB5, pNodeB6, pNodeB7); bool ans = isSameTree(pNodeA1, pNodeB1); if (ans == true) { cout << "Same Tree!" << endl; } else { cout << "Not Same Tree!" << endl; } DestroyTree(pNodeA1); DestroyTree(pNodeB1); return 0; } |
结果输出:
Not Same Tree!
迭代版本:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
#include <iostream>
#include <cstdio> #include <stack> #include <vector> #include "BinaryTree.h" using namespace std; /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ bool isSameTree(TreeNode *p, TreeNode *q) { stack<TreeNode *> s; s.push(p); s.push(q); while (!s.empty()) { p = s.top(); s.pop(); q = s.top(); s.pop(); if (!p && !q) { continue; } if(!p || !q) { return false; } if (p->val != q->val) { return false; } s.push(p->left); s.push(q->left); s.push(p->right); s.push(q->right); } return true; } // 树中结点含有分叉, // 6 // / \ // 7 2 // / \ // 1 4 // / \ // 3 5 int main() { TreeNode *pNodeA1 = CreateBinaryTreeNode(6); TreeNode *pNodeA2 = CreateBinaryTreeNode(7); TreeNode *pNodeA3 = CreateBinaryTreeNode(2); TreeNode *pNodeA4 = CreateBinaryTreeNode(1); TreeNode *pNodeA5 = CreateBinaryTreeNode(4); TreeNode *pNodeA6 = CreateBinaryTreeNode(3); TreeNode *pNodeA7 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNodeA1, pNodeA2, pNodeA3); ConnectTreeNodes(pNodeA2, pNodeA4, pNodeA5); ConnectTreeNodes(pNodeA5, pNodeA6, pNodeA7); TreeNode *pNodeB1 = CreateBinaryTreeNode(6); TreeNode *pNodeB2 = CreateBinaryTreeNode(7); TreeNode *pNodeB3 = CreateBinaryTreeNode(2); TreeNode *pNodeB4 = CreateBinaryTreeNode(1); TreeNode *pNodeB5 = CreateBinaryTreeNode(4); TreeNode *pNodeB6 = CreateBinaryTreeNode(3); TreeNode *pNodeB7 = CreateBinaryTreeNode(5); ConnectTreeNodes(pNodeB1, pNodeB2, pNodeB3); ConnectTreeNodes(pNodeB2, pNodeB4, pNodeB5); ConnectTreeNodes(pNodeB5, pNodeB6, pNodeB7); bool ans = isSameTree(pNodeA1, pNodeB1); if (ans == true) { cout << "Same Tree!" << endl; } else { cout << "Not Same Tree!" << endl; } DestroyTree(pNodeA1); DestroyTree(pNodeB1); return 0; } |
结果输出:
Same Tree
BinaryTree.h:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#ifndef _BINARY_TREE_H_
#define _BINARY_TREE_H_ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; TreeNode *CreateBinaryTreeNode(int value); void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight); void PrintTreeNode(TreeNode *pNode); void PrintTree(TreeNode *pRoot); void DestroyTree(TreeNode *pRoot); #endif /*_BINARY_TREE_H_*/ |
BinaryTree.cpp:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
#include <iostream>
#include <cstdio> #include "BinaryTree.h" using namespace std; /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ //创建结点 TreeNode *CreateBinaryTreeNode(int value) { TreeNode *pNode = new TreeNode(value); return pNode; } //连接结点 void ConnectTreeNodes(TreeNode *pParent, TreeNode *pLeft, TreeNode *pRight) { if(pParent != NULL) { pParent->left = pLeft; pParent->right = pRight; } } //打印节点内容以及左右子结点内容 void PrintTreeNode(TreeNode *pNode) { if(pNode != NULL) { printf("value of this node is: %d\n", pNode->val); if(pNode->left != NULL) printf("value of its left child is: %d.\n", pNode->left->val); else printf("left child is null.\n"); if(pNode->right != NULL) printf("value of its right child is: %d.\n", pNode->right->val); else printf("right child is null.\n"); } else { printf("this node is null.\n"); } printf("\n"); } //前序遍历递归方法打印结点内容 void PrintTree(TreeNode *pRoot) { PrintTreeNode(pRoot); if(pRoot != NULL) { if(pRoot->left != NULL) PrintTree(pRoot->left); if(pRoot->right != NULL) PrintTree(pRoot->right); } } void DestroyTree(TreeNode *pRoot) { if(pRoot != NULL) { TreeNode *pLeft = pRoot->left; TreeNode *pRight = pRoot->right; delete pRoot; pRoot = NULL; DestroyTree(pLeft); DestroyTree(pRight); } } |