[面试题]Amazon: Given two binary trees,if the first tree is subtree of the second one
2014-02-11 11:19
615 查看
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.
两个树,先写另一个函数:isIdentical,
用递归的方式:如何判断两个树是否完全一样呢?那就看从根开始,所有节点的值都相等。
写好了这个,就简单了。 给两个树的根,如何都相等,root就是root2的子树,反之,我们还要查看其他节点,要检查的子树root不变,看left和right树。
继续递归,只要找到一个相等的,那就是有子树。思路其实很简单,就是不要把这个问题放到一个递归里,比较麻烦。 我要是放一起考虑,就感觉会脑残。
public static boolean isSubTree(TreeNode root,TreeNode root2){
if(root==null) return true;
if(root2==null) return false;
if(isIdentical(root,root2)) return true;
else {
return isIdentical(root,root2.left)||isIdentical(root,root2.right);
}
}
public static boolean isIdentical(TreeNode root,TreeNode root2){
if(root==null&&root2==null) return true;
if(root==null||root2==null) return false;
if(root.val==root2.val) {
return isSubTree(root.left,root2.left)&&isSubTree(root.right,root2.right);
}
else return false;
}
主函数test case:
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.
两个树,先写另一个函数:isIdentical,
用递归的方式:如何判断两个树是否完全一样呢?那就看从根开始,所有节点的值都相等。
写好了这个,就简单了。 给两个树的根,如何都相等,root就是root2的子树,反之,我们还要查看其他节点,要检查的子树root不变,看left和right树。
继续递归,只要找到一个相等的,那就是有子树。思路其实很简单,就是不要把这个问题放到一个递归里,比较麻烦。 我要是放一起考虑,就感觉会脑残。
public static boolean isSubTree(TreeNode root,TreeNode root2){
if(root==null) return true;
if(root2==null) return false;
if(isIdentical(root,root2)) return true;
else {
return isIdentical(root,root2.left)||isIdentical(root,root2.right);
}
}
public static boolean isIdentical(TreeNode root,TreeNode root2){
if(root==null&&root2==null) return true;
if(root==null||root2==null) return false;
if(root.val==root2.val) {
return isSubTree(root.left,root2.left)&&isSubTree(root.right,root2.right);
}
else return false;
}
public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x){ val=x; } }
主函数test case:
public static void main(String[] args) { // TODO Auto-generated method stub TreeNode root=new TreeNode(10); root.left=new TreeNode(4); root.right=new TreeNode(6); root.left.right=new TreeNode(30); TreeNode root2=new TreeNode(26); root2.left=new TreeNode(10); root2.left.left=new TreeNode(4); root2.left.right=new TreeNode(6); root2.right=new TreeNode(6); root2.left.left.right=new TreeNode(30); root2.right=new TreeNode(3); root2.right.right=new TreeNode(3); System.out.println(isSubTree(root,root2)); //System.out.println(isIdentical(root,root2)); }
相关文章推荐
- Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe
- ch4.7: given 2 huge Binary tree t1, t2(>1millon nodes), decide if one tree is subtree of another one
- 1.3 Given two strings, write a method to decide if one is a permutation of the other.
- Check if a binary tree is subtree of another binary tree
- Check if the given binary tree is BST or not.
- If you are given two traversal sequences, can you construct the binary tree?
- P17 (*) Split a list into two parts; the length of the first part is given.
- P17 (*) Split a list into two parts; the length of the first part is given.
- FW - check whether tree is balanced or not - find the max and min of one tree- 2013年12月18日13:12:04
- Class JavaLaunchHelper is implemented in both. One of the two will be used. Which one is undefined
- [3752]:Is the one been second-killed first?
- Data Structure Binary Tree: Check if a given Binary Tree is SumTree
- objc[2213]: Class _NSZombie_BSXPCMessage is implemented in both ?? and ??. One of the two will be us
- Lowest Common Ancestor of The Two Nodes in a Binary Tree
- One of the two will be used. Which one is undefined
- 杭电ACM HDU 3752 Is the one been second-killed first?
- (Pod造成的)One of the two will be used. Which one is undefined.
- Windows Azure CloudBlobContainer.CreateIfNotExist : One of the request inputs is out of range.
- hdu 3752 Is the one been second-killed first?
- hdu 3752 Is the one been second-killed first? 菜鸟杯