Skip to content
Y
yTube.SITE v8.0.4
media / worlds / tools / internet oddities
YouTube / video information

LeetCode 3331 | Find Subtree Sizes After Changes | Java | Medium | Tree DFS & | CodeByTushu

Video information, playback and saved actions.

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
Β Β Β Β Β Β 
Videos related to "LeetCode 3331 | Find Subtree Sizes After Changes | Java | Medium | Tree DFS & | CodeByTushu"