LeetCode 3331 | Find Subtree Sizes After Changes | Java | Medium | Tree DFS & | CodeByTushu
Description: π Welcome Back to CodeByTushu!
In todayβs video, we solve LeetCode 3331 β Find Subtree Sizes After Changes using Tree DFS & Ancestor Tracking in Java.
In this problem, we are given a tree rooted at node 0 with n nodes, where each node has a character assigned to it. Simultaneously for each node, its parent is reassigned to its closest ancestor having the same character (or left unchanged if no such ancestor exists). We need to determine the subtree size of every node in the newly formed tree.
We solve this efficiently in linear time by traversing the original tree using an iterative Depth-First Search (DFS) while maintaining an array tracking the deepest ancestor for each of the 26 lowercase English letters. This allows us to reassign parents in O(1) time per node. We then compute the subtree sizes of the transformed tree in a single bottom-up pass using reverse preorder accumulation, achieving optimal O(n) time and O(n) space complexity without risking recursion stack overflow.
π₯ Problem Highlights
β Reassigning node parents simultaneously based on closest identical character ancestor
β Constant-time ancestor lookup using a 26-element tracker array during tree traversal
β Iterative DFS to handle large constraints (n = 10^5) without call stack overflow
β Bottom-up reverse preorder accumulation for linear-time subtree size computation
β Clean, production-ready, and memory-efficient algorithm
β Optimal O(n) time and O(n) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:07 Problem Understanding
03:37 Observation
06:13 Approach
07:52 Code Explanation
10:12 Code Run
12:59 Time Complexity
14:08 Space Complexity
14:59 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/find-subtree-sizes-after-changes
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Tree Traversal Algorithms
Depth-First Search (DFS)
Ancestor Tracking Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is an iterative DFS preferred over recursive DFS when n can be as large as 10^5 on an unbalanced tree?
1οΈβ£ Recursive DFS uses less memory than iterative DFS
2οΈβ£ Deep recursion can trigger a StackOverflowError due to JVM call stack limits
3οΈβ£ Iterative DFS runs with a lower time complexity
4οΈβ£ Recursive DFS cannot track ancestor characters accurately
#LeetCode3331 #LeetCode #FindSubtreeSizesAfterChanges #Java #JavaDSA #Tree #DepthFirstSearch #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description
In todayβs video, we solve LeetCode 3331 β Find Subtree Sizes After Changes using Tree DFS & Ancestor Tracking in Java.
In this problem, we are given a tree rooted at node 0 with n nodes, where each node has a character assigned to it. Simultaneously for each node, its parent is reassigned to its closest ancestor having the same character (or left unchanged if no such ancestor exists). We need to determine the subtree size of every node in the newly formed tree.
We solve this efficiently in linear time by traversing the original tree using an iterative Depth-First Search (DFS) while maintaining an array tracking the deepest ancestor for each of the 26 lowercase English letters. This allows us to reassign parents in O(1) time per node. We then compute the subtree sizes of the transformed tree in a single bottom-up pass using reverse preorder accumulation, achieving optimal O(n) time and O(n) space complexity without risking recursion stack overflow.
π₯ Problem Highlights
β Reassigning node parents simultaneously based on closest identical character ancestor
β Constant-time ancestor lookup using a 26-element tracker array during tree traversal
β Iterative DFS to handle large constraints (n = 10^5) without call stack overflow
β Bottom-up reverse preorder accumulation for linear-time subtree size computation
β Clean, production-ready, and memory-efficient algorithm
β Optimal O(n) time and O(n) auxiliary space complexity
β±οΈ TIMESTAMPS
00:00 Intro
01:07 Problem Understanding
03:37 Observation
06:13 Approach
07:52 Code Explanation
10:12 Code Run
12:59 Time Complexity
14:08 Space Complexity
14:59 Outro
π Problem Folder & Detailed Documentation:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/tree/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes
β Java Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/Solution.java
π Python Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.py
β‘ C++ Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.cpp
π JavaScript Solution:
https://github.com/CodeByTushu/CodeByTushu-LeetCode-Solutions/blob/main/solutions/November/21-November/3331-Find-Subtree-Sizes-After-Changes/solution.js
π LeetCode Source Code
π https://codebytushu.com/leetcode/problem/find-subtree-sizes-after-changes
π All LeetCode Solutions
π https://codebytushu.com/Leetcode/
π± Join CodeByTushu on WhatsApp
Get coding updates, LeetCode solutions, daily questions and more.
https://whatsapp.com/channel/0029Vb8k5mCDeON2Xwlkxe40
β€οΈ Donate
π https://codebytushu.com/Leetcode/donate.php
π₯ Perfect For
LeetCode Medium Problems
Java DSA Preparation
Tree Traversal Algorithms
Depth-First Search (DFS)
Ancestor Tracking Patterns
Time Complexity Optimization
Coding Interview Preparation
Problem Solving
π Follow CodeByTushu
βΆοΈ YouTube: https://www.youtube.com/@codebytushu
πΈ Instagram: https://instagram.com/codebytushu/
πΌ LinkedIn: https://www.linkedin.com/company/codebytushu/
π Website: https://codebytushu.com
π GitHub: https://github.com/CodeByTushu
β€οΈ Donβt Forget To
π Like | π¬ Comment | π Subscribe
π¬ Question of the Day
Why is an iterative DFS preferred over recursive DFS when n can be as large as 10^5 on an unbalanced tree?
1οΈβ£ Recursive DFS uses less memory than iterative DFS
2οΈβ£ Deep recursion can trigger a StackOverflowError due to JVM call stack limits
3οΈβ£ Iterative DFS runs with a lower time complexity
4οΈβ£ Recursive DFS cannot track ancestor characters accurately
#LeetCode3331 #LeetCode #FindSubtreeSizesAfterChanges #Java #JavaDSA #Tree #DepthFirstSearch #DSA #CodingInterview #ProblemSolving #LeetCodeMedium #DailyLeetCode #JavaProgramming #CodeByTushu
π Comment the answer below! π₯Β Close description




