[Java] Leetcode 652. Find Duplicate Subtrees [Binary Tree #10]

  Рет қаралды 7,831

Eric Programming

Eric Programming

Күн бұрын

Пікірлер: 8
@pengx9000
@pengx9000 Жыл бұрын
I'm a little bit confused about this: if we do post-order traversal, why we first append the curNode val (line 44), then append left subtree, last right subtree (15:00), shouldn't be (Left, Right, CurNode)?
@ovidiubalaban2466
@ovidiubalaban2466 Жыл бұрын
It doesn't matter because it's just a String representation which is used to compare values, and it's the same if the CurNode is at the start or at end, you could try it and you will see that it works in both ways. What it does matter is to execute first, the leftNode, then the rightNode, and then to return the curSubtree.
@suryateja6780
@suryateja6780 2 жыл бұрын
What is that chrome extension you are using to draw/write on the tab?
@MeetManga
@MeetManga 2 жыл бұрын
Another solution,the time complexity and space complexity is also N², but the code is shorter, enjoy: class Solution { Map map = new HashMap(); List res = new LinkedList(); public List findDuplicateSubtrees(TreeNode root) { String path = ""; dfs(root, path); return res; } private String dfs(TreeNode root, String path){ if(root == null) return "#"; path += String.join(",", String.valueOf(root.val), dfs(root.left, path), dfs(root.right, path)); map.put(path, map.getOrDefault(path, 0) + 1); if(map.get(path) == 2){ res.add(root); } return path; } }
@xuebindong4803
@xuebindong4803 2 жыл бұрын
love you
@diwyanshukanthwal8669
@diwyanshukanthwal8669 2 жыл бұрын
why inorder will not work on this
@austing.8682
@austing.8682 2 жыл бұрын
Because you need the left serialization and the right serialization before you can create a complete a serialization for the current node.
@rahulmg05
@rahulmg05 2 жыл бұрын
@@austing.8682 Isn't that true for pre order and post order as well ?
[Java] Leetcode 543. Diameter of Binary Tree [Binary Tree #11]
9:40
Eric Programming
Рет қаралды 8 М.
Find Duplicate Subtrees - Leetcode 652 - Python
14:33
NeetCodeIO
Рет қаралды 20 М.
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН
БУ, ИСПУГАЛСЯ?? #shorts
00:22
Паша Осадчий
Рет қаралды 2,9 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
Find Duplicate Subtrees | Leetcode 652 | Binary Trees
22:24
Ayushi Sharma
Рет қаралды 6 М.
Top 7 Algorithms for Coding Interviews Explained SIMPLY
21:22
Codebagel
Рет қаралды 442 М.
5 Steps to Problem Solve Coding Interview Questions
14:10
Eric Programming
Рет қаралды 3,7 М.
LCA - Lowest Common Ancestor
15:56
Errichto Algorithms
Рет қаралды 63 М.
Binary Lifting (Kth Ancestor of a Tree Node)
18:01
Errichto Algorithms
Рет қаралды 100 М.
Lowest Common Ancestor of a binary tree | Leetcode #236
17:29
Turn Off the Vacum And Sit Back and Laugh 🤣
00:34
SKITSFUL
Рет қаралды 2,8 МЛН