leetcode_652. Find Duplicate Subtrees
来源:互联网 发布:seo关键词的布局原则 编辑:程序博客网 时间:2024/06/09 18:46
Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any oneof them.
Two trees are duplicate if they have the same structure with same node values.
Example 1:
1 / \ 2 3 / / \ 4 2 4 / 4The following are two duplicate subtrees:
2 / 4and
4Therefore, you need to return above trees' root in the form of a list.
class Solution { public List<TreeNode> findDuplicateSubtrees(TreeNode root) { List<TreeNode> res = new LinkedList<>(); pos(root,new HashMap<>(),res); return res; } public String pos(TreeNode cur,Map<String,Integer> map,List<TreeNode> res){ if(cur == null)return "#"; String serial = cur.val + ","+pos(cur.left,map,res)+pos(cur.right,map,res); if(map.getOrDefault(serial,0) == 1) res.add(cur); map.put(serial,map.getOrDefault(serial,0)+1); return serial; }}
阅读全文
0 0
- leetcode_652. Find Duplicate Subtrees
- 652. Find Duplicate Subtrees
- 652. Find Duplicate Subtrees
- [leetcode]652. Find Duplicate Subtrees
- leetcode 652. Find Duplicate Subtrees
- Find Duplicate Subtrees 解题心得
- Find Duplicate Subtrees问题及解法
- Leetcode 652. Find Duplicate Subtrees 寻找重复子树 解题报告
- leetcode 652. Find Duplicate Subtrees Map记录 + 深度优先遍历DFS
- find duplicate
- Leetcode: find all duplicate
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- Find the Duplicate Number
- 树结构练习——判断给定森林中有多少棵树
- nowcode练习赛6(B):点权和
- 杂谈-你思考了吗?
- godoc
- oracle数据查询
- leetcode_652. Find Duplicate Subtrees
- StringUtils 判断字符串
- 关于热理新Cordova Hot Code Push Plugin及cordova-hot-code-push-cli当前使用注意事项
- 文章标题
- Setting up Django and your web server with uWSGI and nginx
- allowDefinition='MachineToApplication 错误
- DistanceTransform(距离变换)
- JavaScript总结-细节
- Java基础知识-6、数组