links
New Ket OJ: Sub-structure of the tree
Nine degrees oj:http://ac.jobdu.com/problem.php?pid=1520
GitHub code: 018-Tree substructure
CSDN: The substructure of the offer–018-tree of the sword finger
New Ket OJ |
Nine degrees OJ |
csdn |
GitHub Code |
Child structure of a tree |
1520-The substructure of the tree |
The substructure of the offer–018-tree of the sword finger |
018-The substructure of the tree |
you can also choose to go back to the table of Contents-offer– index of the title set the
Topic Description
Enter two binary tree a,b to determine whether B is a sub structure. Analysis
To find out whether a subtree with the same tree B structure exists in tree A, you can split into two steps:
1. The first step is to find the same node R as the value of the root node of B in tree A;
This is actually the traversal of the tree. You can use recursion to implement
Recursive call Hassubtree traversal binary tree A. If the value of a node is found to be the same as the value of the head node of tree B, then the 2nd step is to determine whether a parent-child relationship exists for the number of two nodes.
2. The second step is to determine whether the subtree with R as the root node in tree A contains the same structure as tree B.
The process is actually to determine whether the two trees correspond to the same node data. This is a recursive process.
First we implement the 2nd step, which is to recursively determine whether the two tree corresponding sections are the same,
The end of recursion is if the previous nodes are the same, the last subtree is empty, and if the parent tree is null, the two trees are exactly the same, and if the parent tree is not NULL, the subtree is part of the parent tree
BOOL Doesparenthavechild (TreeNode *parent, TreeNode *child)
{
if (child = = null) // subtree is null, then must be a subtree
{return
true;
}
else if (parent = = NULL) // subtree is not null, but the father tree is null
{return
false;
} two nodes are not equal in value, then two trees must not be parent-child relationship
if (parent->val!= child->val)
{return
false;
}
else //Otherwise the current node is currently equal, then the recursive judgment Saozi whether the right subtree corresponds to the same node
{return
doesparenthavechild (Parent->left, child-> left)
&& doesparenthavechild (parent->right, child->right);
}
}
Then it's our first step to find out if there is a node in the parent tree that is the same as the root node of the subtree
BOOL Hassubtree (treenode* Parent, treenode* Child)
{
if (child = NULL | | | parent = NULL)
{return
false;< c5/>}
bool result; If the node of the current parent tree is the same as the root node of the subtree, the current position of the parent tree is immediately determined if
(Parent->val = child->val)
{result
= Doesparenthavechild (parent, child);
if (result!= true)
{return
hassubtree (parent->left, child) // lookup from left subtree
| | Hassubtree (parent->right, child); find in the right subtree
}
else {
return
true;
}
}
Code
#include <iostream> using namespace std; Debug Switch #define __tmain main #ifdef __tmain #define DEBUG cout #else #define DEBUG 0 && cout #endif//__tma
In #ifdef __tmain struct TreeNode {int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode (int x): Val (x), left (null), right (null) {}}; #endif//__tmain class Solution {Public:bool hassubtree (treenode* parent, treenode* child) {if = = NULL | |
Parent = = NULL) {return false;
BOOL result; If the node of the current parent tree is the same as the root node of the subtree, the current position of the parent tree is immediately determined if (Parent->val = child->val) {result = Doespare
Nthavechild (parent, child); } if (Result!= true) {return Hassubtree (parent->left, child)//lookup from left subtree | | Hassubtree (parent->right, child);
Look in the right subtree for} else {return true;
}
} BOOL Doesparenthavechild (TreeNode *parent, TreeNode *child) {if (child = = null)//subtree is null, then must be a subtree
{return true;
else if (parent = = NULL)//subtree is not NULL, but the father tree is null {return false;
Two nodes are not equal in value, then two trees must not be parent-child relationship if (parent->val!= child->val) {return false; else//Otherwise the current node is currently equal, then the recursive judgment Saozi the right subtree corresponding to the node is the same {return Doesparenthavechild (parent->
; left, Child->left) && doesparenthavechild (Parent->right, child->right);
}
}
};