Substructure of trees and mirror binary trees

Posted by carmasha on Sat, 06 Jul 2019 01:19:14 +0200

Enter two binary trees A and B to determine whether B is a substructure of A. (ps: We agree that an empty tree is not a substructure of any tree)

Previously, we rebuilt the binary tree according to the order of access in order of order of order of order of order and order of order, so we can also determine a binary tree according to order of order and order of access. It was a recursive construction. Reverse the build process. Determining whether a binary tree is a subtree of another binary tree can be ordered and ordered conveniently. If the preorder and ordered traversing subsequences of a tree contain the preorder and ordered access of another tree, the latter must be the subtree of the former.

class TreeNode{
    var $val;
    var $left = NULL;
    var $right = NULL;
    function __construct($val){
        $this->val = $val;
    }
}
$pre_root1 = array();
$pre_root2 = array();
$in_root1 = array();
$in_root2 = array();
function HasSubtree($pRoot1, $pRoot2)
{
    GLOBAL $pre_root1;
    GLOBAL $pre_root2;
    GLOBAL $in_root1;
    GLOBAL $in_root2;
    preOrder($pRoot1,$pre_root1);
    preOrder($pRoot2,$pre_root2);
    inOrder($pRoot1,$in_root1);
    inOrder($pRoot2,$in_root2);
    $pre_root1_str = implode('',$pre_root1);
    $pre_root2_str = implode('',$pre_root2);
    $in_root1_str = implode('',$in_root1);
    $in_root2_str = implode('',$in_root2);
    if(strpos($pre_root1_str,$pre_root2_str)!==false && strpos($in_root1_str,$in_root2_str)!==false){
        return true;
    }
    else{
        return false;
    }
}
function preOrder($root,&$arr){
    if($root==null){
        return ;
    }
    else{
        $arr[] = $root->val;
        preOrder($root->left, $arr);
        preOrder($root->right, $arr);
    }
}
function inOrder($root,&$arr){
    if($root==null){
        return;
    }
    else{
        inOrder($root->left, $arr);
        $arr[] = $root->val;
        inOrder($root->right, $arr);
    }
}

$root1 = new TreeNode(8);
$root1->left = new TreeNode(8);
$root1->right = new TreeNode(7);
$root1->left->left = new TreeNode(9);
$root1->left->right = new TreeNode(2);

$root2 =  new TreeNode(8);
$root2->left =  new TreeNode(9);
$root2->right =  new TreeNode(2);

echo  HasSubtree($root1, $root2);

This is to judge whether it is a subtree, not a subtree. There are differences between the subtree and the subtree, and the scope of the subtree is wide.

Because the tree itself is a recursive structure, recursive comparison can be considered. It is very convenient to judge whether it is a substructure or not. We can judge whether it is a substructure by comparing it one by one in a recursive way, and we can also compare it recursively.

function HasSubtree($pRoot1, $pRoot2)
{
    if($pRoot1==null&&$pRoot2!=null){//The range of p1 is less than p2
        return false;
    }
    if ($pRoot2==null){//An empty tree is not a substructure of any tree
        return false;
    }
    $flag |= isEqualStruct($pRoot1, $pRoot2);
    if ($flag) {
        return $flag;
    }
    $flag |= HasSubtree($pRoot1->left, $pRoot2);
    if ($flag) {
        return $flag;
    }
    $flag |= HasSubtree($pRoot1->right, $pRoot2);
    return $flag;
}
//The judgment structure is the same, not the judgment tree is the same.
function isEqualStruct($pRoot1,$pRoot2){
    $flag = true;
    if ($pRoot2==null){//p2 is empty, indicating that the range of p1 is greater than or equal to p2
        return true;
    }else if($pRoot1!=null&&$pRoot2!=null){//Not empty then compare, until the comparison is different.
        if ($pRoot1->val==$pRoot2->val){//Further comparison of the same values on the corresponding nodes
            if($flag==true){
                $flag &= isEqualStruct($pRoot1->left,$pRoot2->left);
            }
            if($flag==true){
                $flag &= isEqualStruct($pRoot1->right,$pRoot2->right);
            }
        }else{//If the values are different, you can return false.
            return false;
        }
    }else{//Here, P1 is empty, but p2 is not empty, which means that the range of p2 is larger than p1, and p2 is not a substructure of p1.
        return false;
    }
    return $flag;
}

Topic Description
Operate a given binary tree and transform it into a mirror image of the source binary tree.
Input Description:
Mirror Definition of Binary Tree: Source Binary Tree
8
/ \
6 10
/ \ / \
5 7 9 11
Mirror Binary Tree
8
/ \
10 6
/ \ / \
11 9 7 5

function Mirror(&$root)
{
     if($root==NULL){
        return;
     }
     $t = $root->left;
     $root->left = $root->right;
     $root->right = $t;
     Mirror($root->left);
     Mirror($root->right);
}

.

Topics: less