In preparing for the Amazon interview, one of the topics is this.
A a,b of two trees is known to determine whether B is a subtree of a.
In fact, the topic is not difficult. I don't know why I'm stuck with my own achievements. I remember the last time I wrote a two-fork tree, I was trapped in my achievements. In fact, the traversal is very clear. I do not remember what I did two years ago, this time to write a new one.
I do not know where to look at the instance input inside there are 0, from 0 in my mind deep-rooted. My input is quite strange. Think carefully about the fact that you still do not take 0 into account, because it will affect the results. But not too lazy to change ... I am so ...
First look at the focus of the good, reference to the existing online many blogs. I can't remember what I saw at the time.
The idea is clear:
public boolean Hassubtree (node root1, node Root2) {
//TODO auto-generated a stub
boolean result = false;
if (root1!= null && root2!= NULL)//Exclude special cases where the special root does not exist
{if
(Root1.val = root2.val)/If two root, test
{
result = Doestree1hastree2 (Root1,root2);
}
}
if (result!=true)
{result
= Doestree1hastree2 (Root1.left,root2);
}
if (result!=true)
{result
= Doestree1hastree2 (Root1.right,root2);
}
return result;
}
Private Boolean Doestree1hastree2 (node root1, node Root2) {
//TODO auto-generated Method Stub
Boolean lflag = f Alse;
Boolean rflag = false;
if (Root2 = null) return
true;//test to the end of tree B, fix
if (root1 = null) return
false;//measured to the end of tree A, no way to continue, B must not be a subtree
if (root1.val!= root2.val)
{return
false;//last different
}else
{
Lflag = doestree1hastree2 (root1.left,root2.left);
Rflag = Doestree1hastree2 (Root1.right, root2.right);
Return lflag&&rflag;
}
}
Main function:
Package tree;
Import Tree.BinaryTree.Node; public class Hassubtree {* * * Enter two trees A and b to determine if B is a subtree/public static void main (string[] args) {System.out.prin
TLN ("============================= tree 1 =========================================="); * * 1 *//* 2 3 *///* 4 5 0 6 *///* 0 0
7 0/\ 0 0 * * int[] data01 = {1,2,4,0,0,5,7,0,0,0,3,0,6};
BinaryTree btree = new BinaryTree ();
Btree.root = Btree.buildtree (BTREE.ROOT,DATA01);
Btree.preordertraverse (Btree.root);
System.out.println ("============================= Tree 2 ==========================================");
Int[] data02 = {2,4,0,0,5};
BinaryTree bTree2 = new BinaryTree ();
Btree2.root = Btree2.buildtree (BTREE2.ROOT,DATA02);
Btree2.preordertraverse (Btree2.root);
Boolean flag = false;
Flag = Btree.hassubtree (btree.root,btree2.root); System.out.println ("=======================================\n "+flag);
}
}
Binary Tree section:
Package tree;
Import Java.util.Stack;
Import Tree.BinaryTree.Node;
public class BinaryTree {Node root;
Node parent = null;
public static class node{int val;
Node left;
Node right;
Public Node (int val) {this.left = null;
This.right = null;
This.val = val;
} public BinaryTree () {root = null;
The public node Buildtree (node root, int[] data) {//TODO auto-generated the method stub int i=0;
Node nownode= null;
Node lastnode = null;
if (root = = null) {root = new Node (Data[i]);
Nownode = root;
Lastnode = root;
i++;
while (I < data.length) {//System.out.println ("While:i: +i+" To be deposited is: "+data[i]+" leaf node is: "+nownode.val);
if (Nownode.val = = 0) {if (lastnode.right = = null) {node node = new Node (Data[i]);
lastnode.right = node;
Nownode = Lastnode.right;
i++;
parent = NULL;
Nownode = Findparentsnode (Root,lastnode); }else {//System.out.println ("LAST: "+lastnode.val +" \tnow: "+nownode.val");
while (nownode!=null) {parent = null;
if (nownode.right!=null) {lastnode = Nownode;
parent = NULL;
Nownode = Findparentsnode (Root,lastnode);
}else {//System.out.println ("Out of Bounds:\t" +i);
Node node = new node (data[i]);
Lastnode = Nownode;
nownode.right = node;
Nownode = Nownode.right;
i++;
Break }}}else {if (nownode.left = = null) {//Node node = new Node (data[
I]);
nownode.left = node;
Lastnode = Nownode;
Nownode = Nownode.left;
System.out.println ("I: +i+" to deposit is: "+data[i]+" leaf node is: "+nownode.val);"
i++;
else {node node = new node (data[i]);
nownode.right = node;
Lastnode = Nownode;
Nownode =nownode.right;
i++;
}} return root; Private node Findparentsnode (node ROOT2, node node) {//TODO auto-generated method stub node p = Root2;
if (P!= null) {//System.out.println (P.val);
if (p.left!=null) if (p.left.val = = node.val) parent = p;
if (p.right!=null) if (p.right.val = = node.val) parent = p;
Findparentsnode (P.left,node);
Findparentsnode (P.right,node);
}else return parent;
return to parent;
} void Preordertraverse (node root) {//TODO auto-generated method stub Node p = root;
if (P!= null) {System.out.println (p.val);
Preordertraverse (P.left);
Preordertraverse (P.right);
}else return;
public boolean Hassubtree (node root1, node Root2) {//TODO auto-generated a stub boolean result = False; if (root1!= null && root2!= null) {if (Root1.val = = root2.val) {result = Doestree1hastree2 (Root1,roo
T2);
} if (result!=true) {result = Doestree1hastree2 (ROOT1.LEFT,ROOT2); } if (result!=true) {result = Doestree1hastree2 (ROOT1.RIGHT,ROOT2);
return result; Private Boolean Doestree1hastree2 (node root1, node Root2) {//TODO auto-generated Method Stub Boolean lflag = Fal
Se
Boolean rflag = false; if (Root2 = null) return true;//test to the end of tree B, fix if (ROOT1 = null) return false;//measured to the end of tree A, no way to continue, B must not be a subtree if (RO
Ot1.val!= root2.val) {return false;//the last difference}else {lflag = Doestree1hastree2 (root1.left,root2.left);
Rflag = Doestree1hastree2 (Root1.right, root2.right);
Return lflag&&rflag;
}
}
}