Skip to content
Open

tree #15

Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions Trees/Binary_Tree_Level.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package Trees;

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/

Comment on lines +1 to +18
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Add missing import statements.

The code uses List and ArrayList but the necessary import statements are missing, causing compilation failure.

Add these imports after the package declaration:

 package Trees;
+
+import java.util.ArrayList;
+import java.util.List;

 /**
  * Definition for a binary tree node.
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
package Trees;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
package Trees;
import java.util.ArrayList;
import java.util.List;
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
🤖 Prompt for AI Agents
In Trees/Binary_Tree_Level.java around lines 1 to 18, the file uses List and
ArrayList but lacks the required imports; add the import statements for
java.util.List and java.util.ArrayList (or a single import java.util.*)
immediately after the package Trees; declaration so the code compiles.

class Solution {
List<List<Integer>> res = new ArrayList<>();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix reusability bug by making result a local variable.

The instance variable res causes incorrect behavior when levelOrder is called multiple times on the same Solution instance. The second call will append to the first call's results instead of starting fresh.

Move res to be a local variable in levelOrder and pass it to traverse:

 class Solution {
-    List<List<Integer>> res = new ArrayList<>();

     public List<List<Integer>> levelOrder(TreeNode root) {
+        List<List<Integer>> res = new ArrayList<>();
-        traverse(root, 0);
+        traverse(root, 0, res);
         return res;
     }

-    private void traverse(TreeNode node, int depth) {
+    private void traverse(TreeNode node, int depth, List<List<Integer>> res) {
         if (node == null) {
             return;
         }

         if (res.size() == depth) {
             res.add(new ArrayList<>());
         }

         res.get(depth).add(node.val);
-        traverse(node.left, depth + 1);
-        traverse(node.right, depth + 1);
+        traverse(node.left, depth + 1, res);
+        traverse(node.right, depth + 1, res);
     }

Committable suggestion skipped: line range outside the PR's diff.

🤖 Prompt for AI Agents
In Trees/Binary_Tree_Level.java around line 20, the instance field "res" causes
incorrect reuse across multiple levelOrder calls; make "res" a local variable by
removing the class-level declaration, instantiate List<List<Integer>> res = new
ArrayList<>() at the start of levelOrder, change traverse to accept this res as
a parameter (update its signature and all calls), and have traverse add levels
into the passed-in res; ensure levelOrder returns that local res so each call
starts with a fresh list.


public List<List<Integer>> levelOrder(TreeNode root) {
traverse(root, 0);
return res;
}

private void traverse(TreeNode node, int depth) {
if (node == null) {
return;
}

if (res.size() == depth) {
res.add(new ArrayList<>());
}

res.get(depth).add(node.val);
traverse(node.left, depth + 1);
traverse(node.right, depth + 1);
}


}