数据结构——确定一个二叉树是不是另一个二叉树的子树
2016-12-04 14:17
423 查看
原文地址:Check if a binary tree is subtree of another binary tree | Set 1
已知两个二叉树,看一下第一个树是不是第二个树的子树。一个树T的子树是树S,这个树由是T中的一个节点及其它的下属节点组成的。子树对应的是根节点的话,那就是整个树;子树对应的是任意其他节点的话叫做正常子树(proper subtree)。
例如,在下面的例子中,树S是树T的一个子树。
解决方案:先序遍历树T。对于每一个遍历过的节点,查看这个节点是否等于S的根节点。
输出:
时间复杂度:最坏情况下上述解法的时间复杂度是O(mn),在这里m与n分别是已知两个树中的节点数目。
我们可以在O(n)的时间内解决问题,请参考Check if a binary tree is subtree of another binary tree | Set 2O(n) 的解决方案。
已知两个二叉树,看一下第一个树是不是第二个树的子树。一个树T的子树是树S,这个树由是T中的一个节点及其它的下属节点组成的。子树对应的是根节点的话,那就是整个树;子树对应的是任意其他节点的话叫做正常子树(proper subtree)。
例如,在下面的例子中,树S是树T的一个子树。
Tree 2 10 / \ 4 6 \ 30
Tree 1 26 / \ 10 3 / \ \ 4 6 3 \ 30
解决方案:先序遍历树T。对于每一个遍历过的节点,查看这个节点是否等于S的根节点。
// Java program to check if binary tree is subtree of another binary tree // A binary tree node class Node { int data; Node left, right, nextRight; Node(int item) { data = item; left = right = nextRight = null; } } class BinaryTree { Node root1,root2; /* A utility function to check whether trees with roots as root1 and root2 are identical or not */ boolean areIdentical(Node root1, Node root2) { /* base cases */ if (root1 == null && root2 == null) return true; if (root1 == null || root2 == null) return false; /* Check if the data of both roots is same and data of left and right subtrees are also same */ return (root1.data == root2.data && areIdentical(root1.left, root2.left) && areIdentical(root1.right, root2.right)); } /* This function returns true if S is a subtree of T, otherwise false */ boolean isSubtree(Node T, Node S) { /* base cases */ if (S == null) return true; if (T == null) return false; /* Check the tree with root as current node */ if (areIdentical(T, S)) return true; /* If the tree with root as current node doesn't match then try left and right subtrees one by one */ return isSubtree(T.left, S) || isSubtree(T.right, S); } public static void main(String args[]) { BinaryTree tree = new BinaryTree(); // TREE 1 /* Construct the following tree 26 / \ 10 3 / \ \ 4 6 3 \ 30 */ tree.root1 = new Node(26); tree.root1.right = new Node(3); tree.root1.right.right = new Node(3); tree.root1.left = new Node(10); tree.root1.left.left = new Node(4); tree.root1.left.left.right = new Node(30); tree.root1.left.right = new Node(6); // TREE 2 /* Construct the following tree 10 / \ 4 6 \ 30 */ tree.root2 = new Node(10); tree.root2.right = new Node(6); tree.root2.left = new Node(4); tree.root2.left.right = new Node(30); if (tree.isSubtree(tree.root1, tree.root2)) System.out.println("Tree 2 is subtree of Tree 1"); else System.out.println("Tree 2 is not a subtree of Tree 1"); } } // This code has been contributed by Mayank Jaiswal
输出:
Tree 2 is subtree of Tree 1
时间复杂度:最坏情况下上述解法的时间复杂度是O(mn),在这里m与n分别是已知两个树中的节点数目。
我们可以在O(n)的时间内解决问题,请参考Check if a binary tree is subtree of another binary tree | Set 2O(n) 的解决方案。
相关文章推荐
- 数据结构——确定一个二叉树是不是另一个二叉树的子树(2)
- 递归算法练习与整理(一):判断一个二叉树是不是另一个二叉树的的子结构
- 剑指offer15--判断一个二叉树是不是另一个的子树
- 确定一个二叉树是另一个二叉树的子树
- 【笔试】超大文件里的数据去重排序;将log中MAC地址提取出来;取出数据库表T里第20行到29行的数据;如何判断一个二叉树B是不是二叉树A的子树?
- sqlserver 两个数据库中表的结构不同,如何把一个表中的数据导入到另一个表中
- 检测一个二叉树是否是另一个二叉树的子树
- 判断一个二叉树是否是另一个二叉树的子树
- 数据结构实验之串二:字符串匹配(查看一个字符串是否为另一个字符串的子串)
- 一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程
- 如何判断一个二叉树是否为另一个二叉树的子树
- 数据结构(十二) 二叉树的基本操作 --- 创建一个二叉树 前中后序遍历二叉树
- 两棵树都是空树,或者非空且具有相似的左子树和右子树,则这两棵树是相似的。编写一个函数以确定是否两棵二叉树是相似的
- oracle 同一个数据库实例下 一个用户下面导入到另一个用户表结构还有数据
- 判断一个二叉树是不是另一个二叉树的子结构
- 把一个数据库中的一个表数据迁移到另一个数据库的相同结构的表中
- 判断一个二叉树是否是另一个的子树
- 算法竞赛入门经典:第六章 数据结构基础 6.9 根据二叉树的后序和中序确定前序序列
- 关于结构相同,数据不同的数据库进行一个表数据合并入另一个数据库中的表
- 判断一个二叉树是否是另一个二叉树的子树