1、题目描述:

输入两颗二叉树A,B,判断B是不是A的子结构。

/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x):val(x), left(NULL), right(NULL) { } };*/
class Solution {
public:
    bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
    {
  bool result = false;
        if(pRoot1 != nullptr && pRoot2 != nullptr){
            if(pRoot1->val == pRoot2->val)
                result = Does_tree1_has_tree2(pRoot1, pRoot2);
            if(!result)
                result = HasSubtree(pRoot1->left, pRoot2);
            if(!result)
                result = HasSubtree(pRoot1->right, pRoot2);
        }
        return result;
    }

    bool Does_tree1_has_tree2(TreeNode *root1, TreeNode *root2){
        if(root2 == nullptr) return true;
        if(root1 == nullptr) return false;
        if(root1->val != root2->val) return false;
        return Does_tree1_has_tree2(root1->left, root2->left) &&
            Does_tree1_has_tree2(root1->right, root2->right);
    }
};

2、题目描述:

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:
二叉树的镜像定义:
源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11

镜像二叉树
8
/ \
10 6
/ \ / \
11 9 7 5

/*
struct TreeNode {
 int val;
 struct TreeNode *left;
 struct TreeNode *right;
 TreeNode(int x) :
   val(x), left(NULL), right(NULL) {
 }
};*/
class Solution {
public:
    void Mirror(TreeNode *pRoot) {
  if(pRoot == nullptr) return;
        if(pRoot->left == nullptr && pRoot->right == nullptr) return;

        TreeNode * temp = pRoot->left;
        pRoot->left = pRoot->right;
        pRoot->right = temp;

        if(pRoot->left)
            Mirror(pRoot->left);
        if(pRoot->right)
            Mirror(pRoot->right);
    }
};

如有建议或其他问题,可随时给我们留言。或者到以下链接:

https://github.com/gaobaoru/code_day

Star/Fork/Push 您的代码,开源仓库需要您的贡献。

请查看Coding 题目网址和收藏Accepted代码仓库,进行coding!!!