652. 尋找重復的子樹

給定一棵二叉樹,返回所有重復的子樹。對于同一類的重復子樹,你只需要返回其中任意一棵的根結點即可。

兩棵樹重復是指它們具有相同的結構以及相同的結點值。

示例 1:

? ? ? ? 1
? ? ? ?/ \
? ? ? 2 ? 3
? ? ?/ ? / \
? ? 4 ? 2 ? 4
? ? ? ?/
? ? ? 4
下面是兩個重復的子樹:

? ? ? 2
? ? ?/
? ? 4

? ? 4
因此,你需要以列表的形式返回上述重復子樹的根結點。

來源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/find-duplicate-subtrees
著作權歸領扣網絡所有。商業轉載請聯系官方授權,非商業轉載請注明出處。

解法:

class Solution {
public:vector<TreeNode*> findDuplicateSubtrees(TreeNode* root) {vector<TreeNode*> res;unordered_map<string, int> m;helper(root, m, res);return res;}string helper(TreeNode* node, unordered_map<string, int>& m, vector<TreeNode*>& res) {if (!node) return "#";string str = to_string(node->val) + "," + helper(node->left, m, res) + "," + helper(node->right, m, res);if (m[str] == 1) res.push_back(node);++m[str];return str;}
};

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/385135.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/385135.shtml
英文地址,請注明出處:http://en.pswp.cn/news/385135.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

817. 鏈表組件

給定一個鏈表&#xff08;鏈表結點包含一個整型值&#xff09;的頭結點 head。 同時給定列表 G&#xff0c;該列表是上述鏈表中整型值的一個子集。 返回列表 G 中組件的個數&#xff0c;這里對組件的定義為&#xff1a;鏈表中一段最長連續結點的值&#xff08;該值必須在列表 G…

1121 Damn Single (25 分)

"Damn Single (單身狗)" is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of. Input Specification: Each input file contains one test case. For each case,…

1124 Raffle for Weibo Followers (20 分)

John got a full mark on PAT. He was so happy that he decided to hold a raffle&#xff08;抽獎&#xff09; for his followers on Weibo -- that is, he would select winners from every N followers who forwarded his post, and give away gifts. Now you are suppose…

987. 二叉樹的垂序遍歷

給定二叉樹&#xff0c;按垂序遍歷返回其結點值。 對位于 (X, Y) 的每個結點而言&#xff0c;其左右子結點分別位于 (X-1, Y-1) 和 (X1, Y-1)。 把一條垂線從 X -infinity 移動到 X infinity &#xff0c;每當該垂線與結點接觸時&#xff0c;我們按從上到下的順序報告結點的值…

28. 實現 strStr()

實現 strStr() 函數。 給定一個 haystack 字符串和一個 needle 字符串&#xff0c;在 haystack 字符串中找出 needle 字符串出現的第一個位置 (從0開始)。如果不存在&#xff0c;則返回 -1。 示例 1: 輸入: haystack "hello", needle "ll" 輸出: 2 示例…

1136 A Delayed Palindrome (20 分)

Consider a positive integer N written in standard notation with k1 digits a?i?? as a?k???a?1??a?0?? with 0 for all i and a?k??>0. Then N is palindromic if and only if a?i??a?k?i?? for all i. Zero is written 0 and is also palindrom…

1044 火星數字 (20 分)

火星人是以 13 進制計數的&#xff1a; 地球人的 0 被火星人稱為 tret。地球人數字 1 到 12 的火星文分別為&#xff1a;jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。火星人將進位以后的 12 個高位數字分別稱為&#xff1a;tam, hel, maa, huh, tou, kes, he…

43. 字符串相乘

給定兩個以字符串形式表示的非負整數 num1 和 num2&#xff0c;返回 num1 和 num2 的乘積&#xff0c;它們的乘積也表示為字符串形式。 示例 1: 輸入: num1 "2", num2 "3" 輸出: "6" 示例 2: 輸入: num1 "123", num2 "456&qu…

1045 快速排序 (25 分)

著名的快速排序算法里有一個經典的劃分過程&#xff1a;我們通常采用某種方法取一個元素作為主元&#xff0c;通過交換&#xff0c;把比主元小的元素放到它的左邊&#xff0c;比主元大的元素放到它的右邊。 給定劃分后的 N 個互不相同的正整數的排列&#xff0c;請問有多少個元…

1049 數列的片段和 (20 分)

給定一個正數數列&#xff0c;我們可以從中截取任意的連續的幾個數&#xff0c;稱為片段。例如&#xff0c;給定數列 { 0.1, 0.2, 0.3, 0.4 }&#xff0c;我們有 (0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1, 0.2, 0.3, 0.4) (0.2) (0.2, 0.3) (0.2, 0.3, 0.4) (0.3) (0.3, 0.4) (0…

C++ Priemer目錄索引

序號內容1 【C Primer | 15】虛函數表剖析&#xff08;一&#xff09; 2 【C Priemr | 15】虛函數表剖析&#xff08;二&#xff09; 3 【C Priemr | 15】虛函數表剖析&#xff08;三&#xff09; 4一個C程序執行main函數前和執行完main函數后會發生什么。1 【C Priemr | 15】虛…

1046 劃拳 (15 分)

劃拳是古老中國酒文化的一個有趣的組成部分。酒桌上兩人劃拳的方法為&#xff1a;每人口中喊出一個數字&#xff0c;同時用手比劃出一個數字。如果誰比劃出的數字正好等于兩人喊出的數字之和&#xff0c;誰就贏了&#xff0c;輸家罰一杯酒。兩人同贏或兩人同輸則繼續下一輪&…

多線程順序交替打印ABCD

題目&#xff1a;按照 ABCD的順序交替打印。 1. 測試代碼&#xff1a; #include <iostream> #include <unistd.h> #include <stdlib.h> #include <pthread.h> using namespace std;struct {int t;pthread_mutex_t mutex;pthread_cond_t cond; } tes…

第一個只出現一次的字符

在一個字符串(0<字符串長度<10000&#xff0c;全部由字母組成)中找到第一個只出現一次的字符,并返回它的位置, 如果沒有則返回 -1&#xff08;需要區分大小寫&#xff09;. 解法&#xff1a; class Solution { public:int FirstNotRepeatingChar(string str) {unordered…

《Leetcode》目錄

序號題目題解標記1 43. 字符串相乘 字符串2513. 找樹左下角的值二叉樹3 450. 刪除二叉搜索樹中的節點 二叉樹486. 分隔鏈表鏈表155 155. 最小棧 C題解棧77. 組合C題解回溯算法15.三數之和C題解

一個C++程序執行main函數前和執行完main函數后會發生什么。

總結&#xff1a; main函數執行之前&#xff0c;主要就是初始化系統相關資源&#xff1a; 設置棧指針初始化static靜態和global全局變量&#xff0c;即data段的內容將未初始化部分的賦初值&#xff1a;數值型short&#xff0c;int&#xff0c;long等為0&#xff0c;bool為FALS…

1144 The Missing Number (20 分)

Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (≤10?5??). Then N integers are…

【面試寶典 | 01】面經

字節跳動提前批后端第三面涼經該來的終究會來的

1148 Werewolf - Simple Version (20 分)

Werewolf&#xff08;狼人殺&#xff09; is a game in which the players are partitioned into two parties: the werewolves and the human beings. Suppose that in a game, player #1 said: "Player #2 is a werewolf.";player #2 said: "Player #3 is a h…

算法默寫

序號內容1快速排序算法2堆排序算法3歸并排序算法