LeetCode 3313: Find the Last Marked Nodes in Tree Solution
Master LeetCode problem 3313 (Find the Last Marked Nodes in Tree), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.
3313. Find the Last Marked Nodes in Tree
Problem Explanation
Explanation:
To solve this problem, we can perform a depth-first search (DFS) traversal of the tree while keeping track of the marked nodes. We need to find the last marked nodes in each branch starting from the root. We can achieve this by passing the marked nodes information as we traverse the tree recursively.
Algorithm:
- Implement a recursive DFS function that takes the current node, a list of marked nodes encountered so far, and a list of last marked nodes encountered in each branch.
- In each recursive call, add the current node to the marked nodes list if it is marked.
- Check if the current node is a leaf node. If yes, add it to the last marked nodes list if it is marked.
- Recursively call the function on the left and right child nodes with updated marked nodes list.
- Finally, return the list of last marked nodes encountered in each branch.
Time Complexity:
The time complexity of this solution is O(N), where N is the number of nodes in the tree. We visit each node only once during the DFS traversal.
Space Complexity:
The space complexity of this solution is O(N) due to the recursive calls and the marked nodes list.
: :
Solution Code
class TreeNode {
int val;
TreeNode left, right;
boolean isMarked;
public TreeNode(int val) {
this.val = val;
}
}
class Solution {
public List<TreeNode> findLastMarkedNodes(TreeNode root) {
List<TreeNode> lastMarkedNodes = new ArrayList<>();
dfs(root, new ArrayList<>(), lastMarkedNodes);
return lastMarkedNodes;
}
private void dfs(TreeNode node, List<TreeNode> markedNodes, List<TreeNode> lastMarkedNodes) {
if (node == null) return;
if (node.isMarked) {
markedNodes.add(node);
}
if (node.left == null && node.right == null) {
if (!markedNodes.isEmpty()) {
lastMarkedNodes.add(markedNodes.get(markedNodes.size() - 1));
}
}
dfs(node.left, new ArrayList<>(markedNodes), lastMarkedNodes);
dfs(node.right, new ArrayList<>(markedNodes), lastMarkedNodes);
}
}Try It Yourself
Loading code editor...
Related LeetCode Problems
Frequently Asked Questions
How to solve LeetCode 3313 (Find the Last Marked Nodes in Tree)?
This page provides optimized solutions for LeetCode problem 3313 (Find the Last Marked Nodes in Tree) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.
What is the time complexity of LeetCode 3313 (Find the Last Marked Nodes in Tree)?
The time complexity for LeetCode 3313 (Find the Last Marked Nodes in Tree) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.
Can I run code for LeetCode 3313 on DevExCode?
Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 3313 in Java, C++, or Python.