鱼C论坛

 找回密码
 立即注册
查看: 869|回复: 2

[技术交流] C++刷剑指offer(面试题26. 树的子结构)【广度优先搜索】

[复制链接]
发表于 2020-4-3 19:24:27 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 糖逗 于 2020-5-8 17:29 编辑

题目描述:
  1. 输入两棵二叉树A和B,判断B是不是A的子结构。(约定空树不是任意一个树的子结构)

  2. B是A的子结构, 即 A中有出现和B相同的结构和节点值。

  3. 例如:
  4. 给定的树 A:

  5.      3
  6.     / \
  7.    4   5
  8.   / \
  9.  1   2
  10. 给定的树 B:

  11.    4 
  12.   /
  13.  1
  14. 返回 true,因为 B 与 A 的一个子树拥有相同的结构和节点值。

  15. 示例 1:

  16. 输入:A = [1,2,3], B = [3,1]
  17. 输出:false
  18. 示例 2:

  19. 输入:A = [3,4,5,1,2], B = [4,1]
  20. 输出:true
  21. 限制:

  22. 0 <= 节点个数 <= 10000

  23. 来源:力扣(LeetCode)
  24. 链接:https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof
  25. 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码

  1. #include<iostream>
  2. #include <malloc.h>
  3. #include <vector>
  4. #include <math.h>
  5. #include <queue>
  6. using namespace std;

  7. struct TreeNode{
  8.         int val;
  9.         TreeNode* left;
  10.         TreeNode* right;
  11.         TreeNode(int x): val(x), left(NULL), right(NULL){
  12.         }
  13. };

  14. TreeNode* CreateTree(vector<int> input){
  15.         TreeNode* res = (TreeNode*)malloc(sizeof(TreeNode)*input.size());
  16.         for(int i = 0; i < input.size(); i++){
  17.                 res[i].val = input[i];
  18.                 res[i].left = NULL;
  19.                 res[i].right = NULL;
  20.         }
  21.         for(int i= 0; i < input.size(); i++){
  22.                 if(2*i+1 < input.size()){
  23.                         res[i].left = &res[2*i+1];
  24.                 }
  25.                 if(2*i+2 < input.size()){
  26.                         res[i].right = &res[2*i+2];
  27.                 }
  28.                
  29.         }
  30.         return res;
  31.         
  32. }

  33. void middle(TreeNode* root, vector<vector<int> >& res, int left, int right, int depth){
  34.     if(root == NULL) return;
  35.     int insert = left + (right - left) / 2;
  36.     res[depth][insert] = root->val;
  37.            
  38.     middle(root->left, res, left, insert - 1, depth + 1);
  39.     middle(root->right, res, insert + 1, right, depth + 1);
  40.     }

  41. int treeDepth(TreeNode* root){
  42.     if(root == NULL || root -> val == 0) return 0;
  43.     return max(treeDepth(root->left) + 1, treeDepth(root->right) + 1);
  44. }
  45.       
  46. void PrintTree(TreeNode* root) {
  47.     int depth = treeDepth(root);
  48.     int width = pow(2, depth) - 1;
  49.     vector<vector<int> > res(depth, vector<int>(width, 0));
  50.     middle(root, res, 0, width - 1, 0);
  51.     for(int i = 0; i < res.size(); i++){
  52.                 for(int j = 0; j < res[i].size();j++){
  53.                         if(res[i][j] == 0){
  54.                                 cout  << " ";
  55.                         }
  56.                         else{
  57.                                 cout << res[i][j];
  58.                         }
  59.                        
  60.                 }
  61.                 cout << endl;
  62.         }
  63.         cout << "------------------" << endl;
  64.     }


  65. TreeNode* findNode(TreeNode* A, TreeNode* B){
  66.         if(A == NULL) return A;
  67.         queue <TreeNode*> temp;
  68.         temp.push(A);
  69.         while(!temp.empty()){
  70.                 auto node = temp.front();
  71.                 temp.pop();
  72.                 if(node -> val == B -> val) return node;
  73.                 if(node ->left != NULL) temp.push(node->left);
  74.                 if(node -> right != NULL) temp.push(node -> right);
  75.         }
  76.         return NULL;
  77. }





  78. bool solution(TreeNode*A, TreeNode*B){
  79.         if(A == NULL&& B!= NULL) return false;
  80.     if(A != NULL && B == NULL) return true;
  81.     if(A==NULL && B ==NULL) return true;
  82.     if(A->val != B->val) return false;
  83.     return solution(A->right,B->right) && solution(A->left, B->left);
  84. }

  85. bool isSubStructure(TreeNode*A, TreeNode*B){
  86.     if(A!=NULL && B==NULL) return false;
  87.     TreeNode* node = findNode(A, B);
  88.     return solution(node, B);
  89.    
  90. }

  91. int main(void){
  92.         vector<int> input1, input2;
  93.         cout << "send numbers for the first tree" << endl;
  94.         int number1;
  95.         while(cin >> number1){
  96.                 input1.push_back(number1);
  97.         }
  98.         cin.clear();
  99.         cout << "send numbers for the second tree" << endl;
  100.         int number2;
  101.         while(cin >> number2){
  102.                 input2.push_back(number2);
  103.         }
  104.         
  105.         TreeNode* root1 = CreateTree(input1);
  106.         PrintTree(root1);
  107.         
  108.         TreeNode* root2 = CreateTree(input2);
  109.         PrintTree(root2);
  110.       
  111.                 bool res =  isSubStructure(root1, root2);
  112.                 cout << res << endl;
  113.         return 0;
  114. }
复制代码



注意事项:
1.解题思路:分两步,先找到A中包含B根节点的子节点,然后再递归比较A的子树C和B,判断B是否是C的子树。
2.第一步找子节点用到广度优先搜索。

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2020-4-3 19:25:15 | 显示全部楼层
核心代码:
  1. TreeNode* findNode(TreeNode* A, TreeNode* B){
  2.         if(A == NULL) return A;
  3.         queue <TreeNode*> temp;
  4.         temp.push(A);
  5.         while(!temp.empty()){
  6.                 auto node = temp.front();
  7.                 temp.pop();
  8.                 if(node -> val == B -> val) return node;
  9.                 if(node ->left != NULL) temp.push(node->left);
  10.                 if(node -> right != NULL) temp.push(node -> right);
  11.         }
  12.         return NULL;
  13. }



  14. bool solution(TreeNode*A, TreeNode*B){
  15.         if(A == NULL&& B!= NULL) return false;
  16.     if(A != NULL && B == NULL) return true;
  17.     if(A==NULL && B ==NULL) return true;
  18.     if(A->val != B->val) return false;
  19.     return solution(A->right,B->right) && solution(A->left, B->left);
  20. }

  21. bool isSubStructure(TreeNode*A, TreeNode*B){
  22.     if(A!=NULL && B==NULL) return false;
  23.     TreeNode* node = findNode(A, B);
  24.     return solution(node, B);
  25.    
  26. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-4-3 19:25:48 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-9 06:16

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表