Back to Browse

LeetCode 2467 | Most Profitable Path in a Tree | Python Bob-Timing + DFS Profit | AlgoYogi

66 views
Aug 29, 2025
28:00

Welcome to AlgoYogi! 🚀 Start Your Smart Coding Prep at 👉 https://algoyogi.io In this video, we solve **LeetCode 2467: Most Profitable Path in a Tree** using Python, with a two-pass DFS strategy: 1. **First pass (Bob timing):** From Bob’s starting node, traverse toward the root to record the time Bob reaches each node. 2. **Second pass (Alice DFS):** From root (node 0), DFS along all paths to leaves tracking the time Alice arrives at each node: - If Alice arrives **before** Bob → collect full `amount[i]` - If **same time** → collect half (`amount[i] // 2`) - If Bob arrives **earlier** → collect 0 - Sum values along each path; answer = max sum across all paths 👉 Problem Link: https://leetcode.com/problems/most-profitable-path-in-a-tree/description/ --- ### ⏱ Timestamps 0:00 Introduction 0:20 Problem Statement 1:20 Bob’s Timing Pass (DFS) & Alice’s DFS Salary Calculation 13:00 Python code 27:10 Time & Space Complexity --- ### Why Watch? - Learn how to **handle simultaneous agents (Alice vs Bob)** using arrival times - Apply two-pass DFS with **timing-based logic** for profit calculation - Efficient **O(n)** solution with clear correctness and tree traversal pattern --- 📢 Next Steps: - Explore more tree-based DFS challenges on [AlgoYogi.io](https://algoyogi.io) - Share your iterative, memoized, or edge-case implementations in the comments! #LeetCode #MostProfitablePath #TreeDP #DFS #Python #AlgoYogi #GraphAlgorithms

Download

0 formats

No download links available.

LeetCode 2467 | Most Profitable Path in a Tree | Python Bob-Timing + DFS Profit | AlgoYogi | NatokHD