In view of the subtree of binary tree and the substructure problem of binary tree, the following is summarized as follows:
What is the substructure and subtree of the two-fork tree.
A subtree means a node that contains all the nodes under it, and a two-forked tree of size n has an n-tree, or a subtree with each node as its root. The substructure means that it contains a node that can only take the left subtree or the right subtree, or none.
1, input two binary sorting tree a,b, Judge B is not a subtree. (PS: We agreed that the empty tree is not a subtree of any tree)
A binary sort tree, also known as a binary lookup tree, is either an empty tree or a tree with the following properties:
1 if its left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node;
2 If its right subtree is not empty, the value of all nodes on the right subtree is less than the value of its root node;
3 its left and right subtrees are also two-fork-sorted trees.
Analysis: One, A, B is a two-fork search tree, indicating that the value of each node in the tree is greater than the value of the left child node, and less than the right child node value.
Second, the decision is not a subtree, it is found in tree A and tree b root node corresponding to the node, after the node all the nodes should be consistent.
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode (int x):
val (x), left (null), right (null) {
}
};
BOOL Issametree (treenode* proot1,treenode* pRoot2)
{
if (Proot1==null&&proot2 = NULL)//At the same time reach the leaf node return
true;
else return
false;
if (Proot1->val = = proot2->val) return
issametree (proot1->left,proot2->left) &&issametree ( Proot1->right,proot2->right);
else return
false;
BOOL Hassubtree (treenode* pRoot1, treenode* pRoot2)
{
if (PRoot2 = NULL) return
false;
else if (pRoot1 = NULL) return
false;
if (Proot1->val = = proot2->val) return
issametree (PROOT1,PROOT2);
if (Proot1->val > Proot2->val) return
hassubtree (PROOT1->LEFT,PROOT2);
if (Proot1->val < Proot2->val) return
hassubtree (PROOT1->RIGHT,PROOT2);
}
The function called is Hassubtree, because a, B is a two-fork sorting tree, it can be judged according to the value of the next step is from a Zuozi or right subtree to find, if there is no knowledge of simple two-tree, you can judge the value of the two lines of code or the relationship between the statement, see the following code.
2, the sword refers to the issue of the offer description: input two binary tree a,b, to determine whether B is a sub structure. (PS: We agreed that an empty tree is not a substructure of any tree)
First look at the topic carefully, one, ordinary two-fork tree, there is no other function; the second, the substructure, can be any part of a tree.
Analyze the possibilities, or follow the above ideas to write code, but there are some changes. First of all, the recursive return of the conditions are not the same, when B reached the leaf node, but a did not reach the leaf node, is also correct, this is not the same as the problem. But if a first to the leaf node, it is definitely wrong, return false. Furthermore, because it is not a binary sort tree, the value of the node is not limited, and if the different nodes have the same value, this should be noted. You may find the same value as the B root node, but look down from the node and find that the node does not correspond and do not return false directly. Because there may be other nodes that have the same value, from that node down and b. To set a flag bit, when it is a substructure that returns true and false when it is not a substructure, then search for the next node of a and continue to judge.
Class Solution {public
:
bool Issubtree (treenode* proot1,treenode* pRoot2)
{
if (proot2==null) / /b at the same time or first reach the leaf node return
true;
else if (pRoot1 = NULL) return
false; B is not empty, but a is empty, stating that a first arrives at the leaf node
if (proot1->val = = proot2->val) return
Issubtree (proot1->left,proot2-> left) &&issubtree (proot1->right,proot2->right);
else return
false;
BOOL Hassubtree (treenode* pRoot1, treenode* pRoot2)
{
if (PRoot2 = NULL) return
false;
else if (pRoot1 = NULL) return
false; A first arrives at the leaf node
bool flag = FALSE;
if (Proot1->val = = proot2->val)
flag = Issubtree (PROOT1,PROOT2);
if (!flag)
flag = Hassubtree (proot1->left,proot2) | | Hassubtree (PROOT1->RIGHT,PROOT2);
return flag;
}
;