Spring cleaning 🌳 ✂️ The task is to remove every subtree containing only 0s from a binary tree. The simplest way to achieve that is setting null on nodes without children and with value 0, so the node gets cut off from our tree. I traverse the tree bottom-up (dfs, post-order) because children must be pruned first. This lets me safely check if the current node forms a zero-only subtree. ✨ Pruned nodes don't vanish. They turn unreachable. Pruned nodes get detached by returning null from the recursive call to their parent. The parent's reference (root.left or root.right) becomes null, making the entire zero-subtree unreachable from the main tree structure. In JavaScript, the garbage collector automatically detects these orphaned objects during its next cycle and reclaims their memory, no manual cleanup or deletion required. #javascript #node #interview
Mariusz Najwer’s Post
More Relevant Posts
-
Easter tree? 🌳 🪺 It’s very similar to the LeetCode 103 problem (Binary Tree Zigzag Level Order Traversal), but this time we don’t need to return an array of values. Instead, we need to mutate the tree and apply the changes directly to the nodes. So we traverse the tree level by level, collect all the nodes on each level, and then reverse the values on every odd level. We also have to remember that we don’t want to change the order of the nodes themselves, only their values. That’s why the built-in reverse() method isn’t enough here. We need a custom algorithm that swaps the values directly (Two pointers). #javascript #interview #node
To view or add a comment, sign in
-
-
🚀 *Quick JavaScript Tip: Cleaner Array & Object Checks* Stop using the old-school "length" check to guard your code. ❌ *Old way* : "persona.length > 0 && persona[0]" ✅ *Modern way* (Optional Chaining): "persona?.[0]" 💡 *Why switch?* ✅ *Cleaner* – Less code, better readability ✅ *Safer* – Prevents crashes if "persona" is "null" or "undefined" ✅ *Robust* – Returns "undefined" instead of throwing a "TypeError" 📊 Very useful when working with API responses where data may be unpredictable. 🔹 *Pro Tip* : Works great for deeply nested objects too. Example: "user?.profile?.address?.city" Happy coding! 💻✨
To view or add a comment, sign in
-
📘 𝐉𝐚𝐯𝐚𝐒𝐜𝐫𝐢𝐩𝐭 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐌𝐨𝐝𝐮𝐥𝐞 (𝐁𝐚𝐬𝐢𝐜) 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 7: 𝐒𝐭𝐫𝐢𝐧𝐠 1.What is string? 2.How to declare string? 3.String Declare Special way or Special purpose string? 4.String Check type? 5.String length Check? 6.String Access index? 7.What is empty string? 8.When to use empty string? 9.String is immutable or mutable? 10.String methods? 11.String concatenation? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between slice and substring? 2.What is template literal? 3.Why string is immutable? 4.What are the benefits of Template Literals (Backticks ``)? 5.Why are strings immutable in JavaScript? How does it work in memory? 𝐒𝐞𝐜𝐭𝐢𝐨𝐧 8: 𝐍𝐮𝐦𝐛𝐞𝐫 & 𝐃𝐚𝐭𝐞 1.What is number? 2.Number Types? 3.Math methods? 4.How to find date? 5.Date methods? 🎯 𝐈𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐐𝐮𝐞𝐬𝐭𝐢𝐨𝐧𝐬 (𝐄𝐱𝐭𝐫𝐚) 1.Difference between Math.floor and Math.round? 2.How to generate random number between range? #DotNet #AspNetCore #MVC #FullStack #SoftwareEngineering #ProgrammingTips #DeveloperLife #LearnToCode #JavaScript #JS #JavaScriptTips #JSLearning #FrontendDevelopment #WebDevelopment #CodingTips #CodeManagement #DevTools
To view or add a comment, sign in
-
-
🚀 Mastering Arrays in JavaScript: Understanding this fundamental data structure 📊 Arrays are collections of items stored in a single unit, allowing developers to store multiple values in a single variable. This makes it easy to organize and access related data efficiently in their code. For developers, understanding arrays is crucial as they are widely used in building applications to manage and manipulate data effectively. Let's break it down step by step: 1️⃣ Declare an array using square brackets [ ]. 2️⃣ Assign values to the array elements separated by commas. 3️⃣ Access elements by their index position, starting from 0. Full code example: ```javascript let fruits = ['apple', 'banana', 'orange']; console.log(fruits[0]); // Output: apple ``` Pro tip: Use array methods like 'push' and 'pop' to add or remove elements dynamically. Common mistake to avoid: Forgetting that array indices start at 0, leading to incorrect element access. 🌟 What's your favorite use case for arrays in your projects? Share below! 🌟 🌐 View my full portfolio and more dev resources at tharindunipun.lk #ArraysInJavaScript #DataStructures #WebDevelopment #JavaScriptTips #CodeNewbie #DevelopersCommunity #ArrayMethods #CodingSkills #tharindunipun
To view or add a comment, sign in
-
-
#day53 Headline: 0ms Runtime | Beats 100% of JavaScript Submissions 🚀 Just cleared the "Single Element in a Sorted Array" challenge on LeetCode with $O(\log n)$ time complexity and $O(1)$ space. While a simple XOR approach works in $O(n)$, the real challenge was implementing a binary search that correctly navigates the even/odd index logic to find that one unique element in logarithmic time. There's a specific kind of satisfaction in seeing that "Beats 100%" metric. It's a reminder that in software engineering, the difference between "it works" and "it's optimal" is where the real fun begins. #LeetCode #JavaScript #DataStructures #Algorithms #CodingLife #Optimization #ProblemSolving
To view or add a comment, sign in
-
-
shipped wyscan v1.4.0-beta today. TypeScript and JavaScript agentic systems are now fully supported, on par with Python. for context, wyscan is a static analysis tool that scans your agent codebase and surfaces AFB04 violations before they hit production. AFB04 is the unauthorized action boundary: the point where an agent executes something it was never supposed to be allowed to do. tool calls without policy gates, execution paths with no authorization check, actions that bypass the enforcement layer entirely. wyscan finds them at the code level, before your agent is ever deployed. Until now, that coverage was Python-only. if your stack was TypeScript or JavaScript - LangChain.js, Mastra, any TS/JS agent framework - you were flying blind. v1.4.0-beta closes that gap. wyscan now scans TS/JS agentic systems the same way it handles Python. same detection logic, same CEE-formatted output, same class of findings. if an unauthorized action boundary violation exists in your agent code, it surfaces, regardless of whether you're on the Python or JS/TS side. this release also fixes and upgrades several Python-side limitations from earlier versions. the output is cleaner, the detection is more accurate, and there's less noise in the results. this is beta, so there will be rough edges. but the core detection works, the findings are real, and the coverage is there. if you're building agentic systems in TS or JS and want to know what wyscan finds in your stack, now you can. https://lnkd.in/dny8Q5tF #buildinpublic #agentsecurity #aiagents #llmsecurity #opensource #wyscan #plarix #typescript #javascript #agentic
To view or add a comment, sign in
-
-
🔥 JavaScript Devs — Mutation vs Immutability (The Silent Bug Creator) Hey devs 👋 This one caused me real production bugs… 👉 Mutation in JavaScript. Example: const obj = { value: 1 }; const newObj = obj; newObj.value = 2; 💥 Now BOTH objects changed. 👉 Problem: Shared references → unexpected side effects 💡 What I changed: ✔ Use spread operator ✔ Avoid direct mutation ✔ Treat state as immutable Example: const newObj = { ...obj, value: 2 }; ⚡ Lesson: “Mutation is easy… debugging it is not.” 👉 Senior mindset: Predictable data = predictable code Have you faced mutation bugs before? #javascript #immutability #programmingtips #webdevelopment #frontenddeveloper #backenddeveloper #cleancode #softwareengineering #codingbestpractices #jsdeepdive #learn
To view or add a comment, sign in
-
-
Why Arrays are more than just "lists" in JavaScript ? From past few days im diving deep into JavaScript Arrays and found amazing yet important fact about them.. Key takeaways from my study.. 1 Array Methods: Mutation vs. Non-Mutation Some methods change the original array, while others return a brand new one. Mutators (Change the original): push(), pop(), splice(), sort(), reverse(). Non-Mutators (Return a copy): map(), filter(), slice(), concat(), spread (...). 2 Random Memory Access: Because arrays (or their references in JS) are stored at contiguous locations, we can access any element in O(1) time. |2 | 4 | 10 | 15 | 21 |6 -> Array element 0 - 1 - 2 - 3 - 4 - 5 -> Array Index
To view or add a comment, sign in
-
Javascript: typeof operator ⚡ JavaScript has a tiny operator that reveals BIG truths. It’s called typeof. If you’re new to JavaScript, this operator helps you understand what type of data you’re working with. That’s extremely helpful when debugging or writing safer code. Here’s why developers love using typeof: • It tells you the data type of a variable • It helps debug unexpected values • It works with numbers, strings, booleans, objects, functions, and more • It prevents logic errors in conditions Example: typeof "Hello" // "string" typeof 42 // "number" typeof true // "boolean" typeof undefined // "undefined" typeof {} // "object" 💡 Simple rule: When you're unsure about a value → use typeof. Small operator. Huge debugging power. #JavaScript #WebDevelopment #FrontendDevelopment #LearnToCode #ProgrammingBasics #JavaScriptTips #CodingForBeginners #SoftwareDevelopment #DeveloperCommunity #TechLearning
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development