Recursive algorithm practice and Collation (a): Determine if a binary tree is a sub-structure of another binary tree

Source: Internet
Author: User

Title: Two the node of the fork tree is defined as follows:

struct TreeNode

{

int m_nvalue;

treenode* M_pleft;

treenode* M_pright;

};

Enter two binary trees A and B to determine if tree B is a sub-structure.

For example, the two trees A and B in the following figure, because the structure of a subset of the subtree in a is the same as B, so B is a sub-structure.

1 8
/    \                                               /    \
8 7 9 2
/    \
9 2
/  \
4 7

Analysis: This is a topic for Microsoft campus recruitment in 2010. Binary tree has always been the data structure in Microsoft's interview questions. Readers who are interested in Microsoft must focus on binary trees.

Back to the subject itself. To find out if tree A has the same subtree as the tree b structure, we can divide it into two steps: The first step is to find the same node n as the root node of B in tree A, and the second step is to determine whether the subtree with n as the root node in tree A contains the same structure as tree B.

The first step is to find the same node in tree A as the value of the root node. This is actually the traversal of the tree. The familiar reader of the two-tree data structure naturally knows that we can traverse it recursively, or we can traverse it in a circular way. Because the recursive code implementation is relatively concise, if there is no special request during the interview, we will usually adopt a recursive approach. Here is the reference code:

BOOL Hassubtree (treenode* pTreeHead1, treenode* pTreeHead2)

{

if (PTreeHead1 = = NULL && pTreeHead2! = null) | |

(PTreeHead1 = null && pTreeHead2 = = null))

return false;

if (PTreeHead1 = = NULL && pTreeHead2 = = null)

return true;

Return Hassubtreecore (PTreeHead1, pTreeHead2);

}

BOOL Hassubtreecore (treenode* pTreeHead1, treenode* pTreeHead2)

{

BOOL result = FALSE;

if (Ptreehead1->m_nvalue = = Ptreehead2->m_nvalue)

{

result = Doestree1haveallnodesoftree2 (PTreeHead1, pTreeHead2);

}

if (!result && ptreehead1->m_pleft! = NULL)

result = Hassubtreecore (Ptreehead1->m_pleft, pTreeHead2);

if (!result && ptreehead1->m_pright! = NULL)

result = Hassubtreecore (Ptreehead1->m_pright, pTreeHead2);

return result;

}

In the above code, we recursively call Hassubtreecore to traverse the binary tree A. If you find that the value of a node is the same as the value of the head node of tree B, call Doestree1haveallnodeoftree2 to make a second decision.

In the interview, we must pay attention to the boundary condition check, namely checks the null pointer. When tree A or tree B is empty, the corresponding output is defined. If you do not check and do the appropriate processing, the program is very easy to collapse, this is a very taboo in the interview. Since there is no need to do a boundary check in each recursion (every time the recursion is checked and the unnecessary time overhead is added), the above code is recursively traversed in the Hassubtreecore only after the boundary check is made in the Hassubtree.

Next, consider the second step and determine if the subtree with n as the root node in tree A has the same structure as tree B. Similarly, we can consider the idea of recursion: if the value of the node n is not the same as the root node of tree B, then the subtree and tree B with n as the root node certainly do not have the same node, and if their values are the same, the values of their respective left and right nodes are judged recursively. The termination condition of recursion is that we reach the leaf node of tree A or tree B. The reference code is as follows:

BOOL Doestree1haveallnodesoftree2 (treenode* pTreeHead1, treenode* pTreeHead2)

{

if (pTreeHead2 = = NULL)

return true;

if (PTreeHead1 = = NULL)

return false;

if (ptreehead1->m_nvalue! = ptreehead2->m_nvalue)

return false;

Return Doestree1haveallnodesoftree2 (Ptreehead1->m_pleft, Ptreehead2->m_pleft) &&

Doestree1haveallnodesoftree2 (Ptreehead1->m_pright, ptreehead2->m_pright);

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.