zoukankan      html  css  js  c++  java
  • LC 652. Find Duplicate Subtrees

    Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them.

    Two trees are duplicate if they have the same structure with same node values.

    Example 1:

            1
           / 
          2   3
         /   / 
        4   2   4
           /
          4
    

    The following are two duplicate subtrees:

          2
         /
        4
    

    and

        4
    

    Therefore, you need to return above trees' root in the form of a list.

    Runtime: 40 ms, faster than 18.69% of C++ online submissions for Find Duplicate Subtrees.

    考的是怎么把树序列化表示,我的写法比较繁琐,运行时间也比较长。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    private:
      unordered_map<string,int> map;
    public:
      vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
        vector<TreeNode*> ret;
        helper(root, ret);
        //for(auto it : map) cout << it.first << endl;
        return ret;
      }
      string helper(TreeNode* root, vector<TreeNode*> & ret){
        if(!root) return "";
        string rootval = to_string(root->val);
        string tmp = rootval;
        if(!root->left && root->right){
          tmp = rootval + " Null " + helper(root->right, ret);
        }else if(root->left && !root->right){
          tmp = rootval + " " + helper(root->left,ret) + " Null ";
        } else if (root->left && root->right){
          tmp = rootval + " " + helper(root->right,ret) + " " + helper(root->left,ret);
        }
        //if(root->val == 4) cout << tmp << endl;
        if(map.count(tmp)) {
          if(map[tmp] == 1) {
            ret.push_back(root);
            map[tmp]++;
          }
        }else {
          map[tmp] = 1;
        }
        return tmp;
      }
    };

    下面是写的比较顺的一种。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    
    // We can serialize each subtree. Perform a depth-first search, where the recursive function returns the serialization of the tree. At each node, record the result in a map, and analyze the map after to determine duplicate subtrees.
    class Solution {
    public:
        vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {
            
            //store count of each serialized tree
            unordered_map<string, int>mymap;
            vector<TreeNode*> res;
    
            DFS(root,mymap,res);
            return res; 
        }
        
        string DFS(TreeNode* root, unordered_map<string, int> &mymap, vector<TreeNode*> &res){
            if(!root){
                return "#";
            }
            
            string s = to_string(root->val) + "," + DFS(root->left, mymap, res) + "," + DFS(root->right, mymap, res);
            if(++mymap[s]==2)
                res.push_back(root);
            return s;       
        }
    };

    更有人用了bit,惊了。

          long key=((static_cast<long>(node->val))<<32 | helper(node->left, ans)<<16 | helper(node->right, ans));
  • 相关阅读:
    Linux下的crontab定时执行任务命令详解
    TP5使用Composer安装PhpSpreadsheet类库实现导入导出
    在本地创建分支并发布到远程仓库
    Linux中文件的可读,可写,可执行权限的解读以及chmod,chown,chgrp命令的用法
    crontab 定时写法整理
    Linux && Windows下基于ThinkPHP5框架实现定时任务(TP5定时任务)-结合Crontab任务
    Echarts环形图、折线图通过ajax动态获取数据
    javascript另类方法高效实现htmlencode()与htmldecode()函数,附带PHP请求完整操作
    PHP获取本月开始、结束时间,近七天所有时间
    关于sql中case when用法
  • 原文地址:https://www.cnblogs.com/ethanhong/p/10177263.html
Copyright © 2011-2022 走看看