Skip to content

Commit b6f2545

Browse files
committed
Runtime: 1783 ms (Top 5.02%) | Memory: 64.5 MB (Top 11.80%)
1 parent 76532d9 commit b6f2545

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
1+
// Runtime: 1783 ms (Top 5.02%) | Memory: 64.5 MB (Top 11.80%)
12
/**
23
* Definition for a binary tree node.
34
* public class TreeNode {
4-
* int val;
5-
* TreeNode left;
6-
* TreeNode right;
7-
* TreeNode() {}
8-
* TreeNode(int val) { this.val = val; }
9-
* TreeNode(int val, TreeNode left, TreeNode right) {
10-
* this.val = val;
11-
* this.left = left;
12-
* this.right = right;
13-
* }
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode() {}
9+
* TreeNode(int val) { this.val = val; }
10+
* TreeNode(int val, TreeNode left, TreeNode right) {
11+
* this.val = val;
12+
* this.left = left;
13+
* this.right = right;
14+
* }
1415
* }
1516
*/
1617
class Solution {
@@ -19,40 +20,38 @@ public List<TreeNode> findDuplicateSubtrees(TreeNode root) {
1920
HashSet<String> hashes = new HashSet<String>();
2021
HashSet<String> added = new HashSet<String>();
2122

22-
2323
// for each node, perform a dfs traversal and generate a hash
24-
// check if the hash already exists in a set,
24+
// check if the hash already exists in a set,
2525
// if it does, get the treenode and add it to the list
2626
Stack<TreeNode> s = new Stack<TreeNode>();
27-
27+
2828
s.add(root);
2929
while(!s.isEmpty()){
3030
TreeNode tmp = s.pop();
3131
dfs(tmp, "", tmp, list, hashes, added);
32-
32+
3333
if(tmp.left != null){
3434
s.add(tmp.left);
3535
}
3636
if(tmp.right != null){
3737
s.add(tmp.right);
3838
}
3939
}
40-
40+
4141
return list;
42-
42+
4343
}
44-
44+
4545
public void dfs(TreeNode parent, String hash, TreeNode root, List<TreeNode> list, HashSet<String> set, HashSet<String> added){
46-
46+
4747
Stack<TreeNode> stack = new Stack<TreeNode>();
48-
48+
4949
stack.add(root);
5050
//String hash = "";
5151
hash += root.val + "ROOT,";
5252
while(!stack.isEmpty()){
5353
TreeNode tmp = stack.pop();
5454
//hash += tmp.val + ",";
55-
5655

5756
if(tmp.left != null){
5857
hash += tmp.left.val + "L,";
@@ -80,7 +79,7 @@ public void dfs(TreeNode parent, String hash, TreeNode root, List<TreeNode> list
8079
}
8180
return;
8281
}
83-
82+
8483
}
8584
}
86-
}
85+
}

0 commit comments

Comments
 (0)