检测一个二叉树是否是另一个二叉树的子树
来源:互联网 发布:淘宝需要注册公司吗 编辑:程序博客网 时间:2024/05/05 17:16
Given two binary trees, check if the first tree is subtree of the second one. A subtree of a tree T is a tree S consisting of a node in T and all of its descendants in T.
The subtree corresponding to the root node is the entire tree; the subtree corresponding to any other node is called a proper subtree.
For example, in the following case, Tree1 is a subtree of Tree2.
给定两个二叉树,检测一个是否另一个的子树。例如下面的例子,Tree1是Tree2的子树:
Tree1 x / \ a b \ c Tree2 z / \ x e / \ \ a b k \ c
这里讨论的是O(N)的方案。这个想法是基于前序、中序(后序)可以唯一标识一个二叉树。如果树S前序、中序遍历后存储都是树T遍历存储后的字串,那么就是子树啦。
答案也比较直接:
#include <iostream>#include <cstring>using namespace std;#define MAX 100 // Structure of a tree nodestruct Node{ char key; struct Node *left, *right;}; // A utility function to create a new BST nodeNode *newNode(char item){ Node *temp = new Node; temp->key = item; temp->left = temp->right = NULL; return temp;} // A utility function to store inorder traversal of tree rooted// with root in an array arr[]. Note that i is passed as referencevoid storeInorder(Node *root, char arr[], int &i){ if (root == NULL) return; storeInorder(root->left, arr, i); arr[i++] = root->key; storeInorder(root->right, arr, i);} // A utility function to store preorder traversal of tree rooted// with root in an array arr[]. Note that i is passed as referencevoid storePreOrder(Node *root, char arr[], int &i){ if (root == NULL) return; arr[i++] = root->key; storePreOrder(root->left, arr, i); storePreOrder(root->right, arr, i);} /* This function returns true if S is a subtree of T, otherwise false */bool isSubtree(Node *T, Node *S){ /* base cases */ if (S == NULL) return true; if (T == NULL) return false; // Store Inorder traversals of T and S in inT[0..m-1] // and inS[0..n-1] respectively int m = 0, n = 0; char inT[MAX], inS[MAX]; storeInorder(T, inT, m); storeInorder(S, inS, n); inT[m] = '\0', inS[n] = '\0'; // If inS[] is not a substring of preS[], return false if (strstr(inT, inS) == NULL) return false; // Store Preorder traversals of T and S in inT[0..m-1] // and inS[0..n-1] respectively m = 0, n = 0; char preT[MAX], preS[MAX]; storePreOrder(T, preT, m); storePreOrder(S, preS, n); preT[m] = '\0', preS[n] = '\0'; // If inS[] is not a substring of preS[], return false // Else return true return (strstr(preT, preS) != NULL);} // Driver program to test above functionint main(){ Node *T = newNode('z'); T->left = newNode('x'); T->right = newNode('e'); T->left->left = newNode('a'); T->left->right = newNode('b'); T->right->right = newNode('k'); T->left->left->right = newNode('c'); Node *S = newNode('x'); S->left = newNode('a'); S->right = newNode('b'); S->left->right = newNode('c'); if (isSubtree(T, S)) cout << "Yes: S is a subtree of T"; else cout << "No: S is NOT a subtree of T"; return 0;}
0 0
- 检测二叉树是否是另一个二叉树的子树
- 检测一个二叉树是否是另一个二叉树的子树
- 判断一个二叉树是否是另一个的子树
- 确定一个二叉树是另一个二叉树的子树
- 判断一个二叉树是否是另一个二叉树的子树
- 判断一个二叉树是否是另一个二叉树的子树
- 求一个二叉树是否为另一个二叉树的子树
- 如何判断一个二叉树是否为另一个二叉树的子树
- 百度笔试题:判断一个二叉树是否是另一颗二叉树的子树
- 剑指offer15--判断一个二叉树是不是另一个的子树
- 2个二叉树A、B,树的每个节点上都有一个整数值。如何判断A与B是否其中一个是另一个的子树?
- 判断一个树是否是另一个树的子树
- LintCode 判断一个二叉树树是否是另一个二叉树的子书
- 数据结构——确定一个二叉树是不是另一个二叉树的子树
- 数据结构——确定一个二叉树是不是另一个二叉树的子树(2)
- 判断是否是子树 和 判断二叉树是否平衡
- 判断一棵二叉树是否是二叉树的子树
- 判断一棵二叉树是否是另一棵二叉树的子树
- uva 11290 - Gangs(卡特兰数)
- MiniGUI的三种运行模式
- gdb的一些常规使用
- 一点php的知识
- 快速开发11之多次点击事件实现功能的万能代码
- 检测一个二叉树是否是另一个二叉树的子树
- StringBuilder
- 基本数据类型对象包装类
- UVa 10742 - The New Rule in Euphomia
- 关于Android屏幕横竖之间的切换,研究心得。
- 基本数据类型对象包装类新特性
- 集合框架(体系概述)-共性方法-迭代器
- javaScript中闭包函数与this对象的应用
- openstack root-wrap