題目地址:
https://leetcode-cn.com/probl...
題目描述:
給定一棵二叉樹,返回全部重複的子樹。對於同一類的重複子樹,你只須要返回其中任意一棵的根結點便可。java
兩棵樹重複是指它們具備相同的結構以及相同的結點值。node
示例 1:code
1 / \ 2 3 / / \ 4 2 4 / 4
下面是兩個重複的子樹:leetcode
2 / 4
和get
4
所以,你須要以列表的形式返回上述重複子樹的根結點。hash
解答:
如何判斷兩棵樹是重複的?只要兩棵樹的先序(各類序均可以)遍歷結果是同樣的,那麼這兩棵樹就是重複的?
不必定!!!it
2 / 4
和
2
\
4
它們的先序遍歷結果就是相同的,可是並不重複。爲何?由於遍歷的時候忽略掉了空樹的位置。
可是若是先序訪問這個樹的時候保留空樹(也就是訪問空樹),那麼此時就成立了!先序遍歷樹並
且對它進行序列化,空樹的序列化結果爲" ",而對於任意節點root它的序列化結果爲"root.val 左子樹序列化 右子樹序列化"。這裏再用hashmap存儲,鍵:序列化結果,值:樹節點組成的鏈表。最後序列化完,遍歷這個hashmap,找到hashmap中,值(鏈表)長度大於1的位置,把這個位置鏈表的第一個節點放入答案集,按照這個思路就能理解下面的代碼:
java ac代碼:io
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { HashMap<String,List<TreeNode>> map = new HashMap(1000); public List<TreeNode> findDuplicateSubtrees(TreeNode root) { serialize(root); List<TreeNode> ans = new ArrayList(1000); for(Map.Entry<String,List<TreeNode>> entry:map.entrySet()) if(entry.getValue().size()>1) ans.add(entry.getValue().get(0) ); return ans; } public String serialize(TreeNode root) { if(root == null)return " "; String temp = root.val+" "+serialize(root.left)+" "+serialize(root.right); if(map.get(temp) == null){ List<TreeNode> list = new LinkedList(); list.add(root); map.put(temp,list); } else map.get(temp).add(root); return temp; } }