Same Tree LeetCode

What am I doing wrong here
17 Replies
JavaBot
JavaBot5mo ago
This post has been reserved for your question.
Hey @Rag...JN 🌌 🦡 👽 💰! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
Rag...JN 🌌 🦡 👽 💰 🐊
class Solution {
List<Integer> treeElements1 = new ArrayList<>();
List<Integer> treeElements2 = new ArrayList<>();

public boolean isSameTree(TreeNode p, TreeNode q) {
inOrderTraversal(p, treeElements1);
inOrderTraversal(q, treeElements2);
System.out.println("tree 1");
printList(treeElements1);
System.out.println("tree 2");
printList(treeElements2);
if (treeElements1.equals(treeElements2))
return true;
else
return false;
}

public static void printList(List<Integer> list) {
for (Integer element : list) {
System.out.println(element);
}
}

public void inOrderTraversal(TreeNode root, List<Integer> list) {
if (root != null) {

if (root.left != null) {
inOrderTraversal(root.left, list);
}
list.add(root.val);
if (root.right != null) {
inOrderTraversal(root.right, list);
}

}
if(root = null){

}
}
}
class Solution {
List<Integer> treeElements1 = new ArrayList<>();
List<Integer> treeElements2 = new ArrayList<>();

public boolean isSameTree(TreeNode p, TreeNode q) {
inOrderTraversal(p, treeElements1);
inOrderTraversal(q, treeElements2);
System.out.println("tree 1");
printList(treeElements1);
System.out.println("tree 2");
printList(treeElements2);
if (treeElements1.equals(treeElements2))
return true;
else
return false;
}

public static void printList(List<Integer> list) {
for (Integer element : list) {
System.out.println(element);
}
}

public void inOrderTraversal(TreeNode root, List<Integer> list) {
if (root != null) {

if (root.left != null) {
inOrderTraversal(root.left, list);
}
list.add(root.val);
if (root.right != null) {
inOrderTraversal(root.right, list);
}

}
if(root = null){

}
}
}
Rag...JN 🌌 🦡 👽 💰 🐊
Failing test case
No description
Kyo-chan
Kyo-chan5mo ago
You're flattening the trees into Lists, then comparing the Lists It could work if you ensured the List you flatten to were canonically representing the trees, but you don't. You produce the same list with a missing left node and a missing right node.
Rag...JN 🌌 🦡 👽 💰 🐊
yah how to canonically represent the tree?
Kyo-chan
Kyo-chan5mo ago
You could do it the same as they do in the input part Here when there is a missing node in the middle they insert null
Rag...JN 🌌 🦡 👽 💰 🐊
yah I tried that then also it generated the same tree
Kyo-chan
Kyo-chan5mo ago
The same list you mean?
Rag...JN 🌌 🦡 👽 💰 🐊
yah it turned out a same list
Kyo-chan
Kyo-chan5mo ago
That's not an attempt to add nulls. You just modified where you insert the root
Rag...JN 🌌 🦡 👽 💰 🐊
alright I figured it out
public void inOrderTraversal(TreeNode root, List<Integer> list) {
if (root != null) {

if (root.left != null) {
inOrderTraversal(root.left, list);

} else {
list.add(null);
}
list.add(root.val);
if (root.right != null) {
inOrderTraversal(root.right, list);

} else {
list.add(null);
}

inOrderTraversal(root.right, list);

}

}
public void inOrderTraversal(TreeNode root, List<Integer> list) {
if (root != null) {

if (root.left != null) {
inOrderTraversal(root.left, list);

} else {
list.add(null);
}
list.add(root.val);
if (root.right != null) {
inOrderTraversal(root.right, list);

} else {
list.add(null);
}

inOrderTraversal(root.right, list);

}

}
Kyo-chan
Kyo-chan5mo ago
I just noticed your list is not like the one they put in input box. I'm not sure it will be canonical this way
Rag...JN 🌌 🦡 👽 💰 🐊
is the better approach?
No description
Rag...JN 🌌 🦡 👽 💰 🐊
This reaches till the end of the leaf node of one of the tree right Then it check if the values are null, one is not null or no even
Kyo-chan
Kyo-chan5mo ago
I mean, it's the most direct approach. Trees are recursive structures, so it checks recursively and directly.
JavaBot
JavaBot5mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server