Binary Tree Maximum Path Sum (Animated Walkthrough) (LeetCode)

AlgosWithMichael January 24, 2021
Video Thumbnail
AlgosWithMichael Logo

AlgosWithMichael

View Channel

About

Hi! My name is Michael. I make coding tutorials to help people pass their technical interviews. I release videos each week and plan to branch out to project videos as well so stay tuned!

Video Description

Binary Tree Maximum Path Sum is a popular LeetCode problem involving the knowledge of recursion, binary trees, and postorder traversal. This problem is asking at companies including Amazon, Google, Microsoft, Facebook, and Bloomberg. For this problem, we are given a binary tree and must find the maximum path sum in the tree. A path sum is the sum of the values in a path of the binary tree. The binary tree can have negative numbers, thus our maximum does not necessarily have to be positive. To solve this efficiently, we must traverse each node in the tree computing the max sum at each step. If we encounter a negative number, we will bound that value to a zero to ensure that we always get the absolute maximum. The time complexity of the solution will be linear because we must does each node in the tree in our traversal. The space complexity is going to be O(H) where H is the height of our tree. Since we are using recursion, in the worst case, we will have H recursive calls which will fill up our stack space. Check out my interview prep platform for learning the patterns! 📢 Interview Prep Platform: https://algoswithmichael.com 🔗 Social 🔗 🎧 Join the community Discord: https://discord.gg/aVWsAaaCtT 💰 Support me on Patreon: https://www.patreon.com/michaelmuinos 🔗Follow me on LinkedIn: https://www.linkedin.com/in/michael-muinos 📂Follow me on Github: https://github.com/MichaelMuinos ⭐️ Timestamps ⭐️ 00:00 - Intro 00:19 - Problem Overview 02:22 - Algorithm Walkthrough 07:35 - Code Walkthrough 10:52 - Time & Space Complexity

You May Also Like

Upgrade Your Tech Today

AI-recommended products based on this video

Loading...
Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

(2,272)
$43.99
PrimeFREE delivery Sat, Jun 14
200+ bought in past month
Loading...
45W Super Fast Charger USBC Charger, 2Pack 10FT Samsung Charger Fast Charging Cord Cable& Samsung Type C Charger Block for Galaxy S25 S24 S23 S22 S21 Ultra/Plus, Tab S10 S9 S8 S7,iPad,Android Phone

45W Super Fast Charger USBC Charger, 2Pack 10FT Samsung Charger Fast Charging Cord Cable& Samsung Type C Charger Block for Galaxy S25 S24 S23 S22 S21 Ultra/Plus, Tab S10 S9 S8 S7,iPad,Android Phone

(52)
$22.79
FREE delivery Fri, Oct 3 on your first order
1K+ bought in past month
Loading...
Samsung Galaxy Tab S10 Ultra Gray 256GB - AI Tablet, 14.6", AMOLED 2X, Anti-Reflection, 420 Nits, Vapor Chamber, S Pen Included (CAD Version & Warranty)

Samsung Galaxy Tab S10 Ultra Gray 256GB - AI Tablet, 14.6", AMOLED 2X, Anti-Reflection, 420 Nits, Vapor Chamber, S Pen Included (CAD Version & Warranty)

(77)
$1,099.99$869.90
FREE delivery Wed, Aug 27
50+ bought in past month
Loading...
Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

(2,272)
$43.99
PrimeFREE delivery Sat, Jun 14
200+ bought in past month
Loading...
Super Fast Charger Type C, 2 Pack 25W USB C Wall Charger Fast Charging for Samsung Galaxy S25 Edge/S25 Ultra/S25/S25+/S24/S24+/S24 Ultra/S23/S22/S21 /S20/Note 20/10 with 10FT Long Type C Charging Cord

Super Fast Charger Type C, 2 Pack 25W USB C Wall Charger Fast Charging for Samsung Galaxy S25 Edge/S25 Ultra/S25/S25+/S24/S24+/S24 Ultra/S23/S22/S21 /S20/Note 20/10 with 10FT Long Type C Charging Cord

(1,440)
$22.49
FREE delivery Sat, Jun 14 on your first order
100+ bought in past month
Loading...
Galaxy S24 S25 USB C Charging Block 25W PD Super Fast Charger Type C Wall Plug Adapter Quick Charge for Samsung Galaxy S25/S24/S23/S22/S21/S20/Z Fold 3/4/5/Note20/iPhone 15/16/iPad/Tablet/Watch-1Pack

Galaxy S24 S25 USB C Charging Block 25W PD Super Fast Charger Type C Wall Plug Adapter Quick Charge for Samsung Galaxy S25/S24/S23/S22/S21/S20/Z Fold 3/4/5/Note20/iPhone 15/16/iPad/Tablet/Watch-1Pack

(7,777)
$12.99
FREE delivery Sat, Jun 14 on your first order
100+ bought in past month
Loading...
SanDisk 128GB 2-Pack microSDXC Card, Licensed for Nintendo Switch (2x128GB) - SDSQXAO-128G-GN6Z2

SanDisk 128GB 2-Pack microSDXC Card, Licensed for Nintendo Switch (2x128GB) - SDSQXAO-128G-GN6Z2

(322,464)
$48.90
PrimeFREE delivery Sat, Jun 14
Loading...
SanDisk 256GB Ultra microSDXC UHS-I Memory Card with Adapter - Up to 150MB/s, C10, U1, Full HD, A1, MicroSD Card - SDSQUAC-256G-GN6MA

SanDisk 256GB Ultra microSDXC UHS-I Memory Card with Adapter - Up to 150MB/s, C10, U1, Full HD, A1, MicroSD Card - SDSQUAC-256G-GN6MA

(217,850)
$24.63$19.70
FREE delivery Wed, Aug 20 on your first order
5K+ bought in past month
Loading...
SanDisk 64GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXU-064G-GN4IN

SanDisk 64GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXU-064G-GN4IN

(71,074)
$21.99$16.59
FREE delivery Sun, Aug 10 on your first order
500+ bought in past month
Loading...
SanDisk 128GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXD-128G-GN4IN
Best Seller

SanDisk 128GB Extreme PRO SDXC UHS-I Memory Card - C10, U3, V30, 4K UHD, SD Card - SDSDXXD-128G-GN4IN

(71,074)
$29.99$26.61
FREE delivery Sun, Aug 10 on your first order
4K+ bought in past month
Loading...
Spigen Case for Galaxy S25 Case, Tough Armor (Ai) MagFit [Wireless Charging] Compatible with MagSafe - Black

Spigen Case for Galaxy S25 Case, Tough Armor (Ai) MagFit [Wireless Charging] Compatible with MagSafe - Black

(2,126)
$28.99
FREE delivery Sat, Jun 14 on your first order
500+ bought in past month
Loading...
Spigen Case for iPhone 16 Case, Tough Armor Designed for Apple iPhone 16 - Black

Spigen Case for iPhone 16 Case, Tough Armor Designed for Apple iPhone 16 - Black

(126)
$22.99
FREE delivery Sat, Jun 14 on your first order
50+ bought in past month
Loading...
Spigen Case for iPhone 16 Pro Case, Tough Armor [MagFit] Compatible with MagSafe - Black

Spigen Case for iPhone 16 Pro Case, Tough Armor [MagFit] Compatible with MagSafe - Black

(341)
$32.99
FREE delivery Sat, Jun 14 on your first order
Loading...
Spigen Case for iPhone 16 Plus Case, Tough Armor (AI) [MagFit] Compatible with MagSafe - Black

Spigen Case for iPhone 16 Plus Case, Tough Armor (AI) [MagFit] Compatible with MagSafe - Black

(818)
$29.99
FREE delivery Sat, Jun 14 on your first order
Loading...
Girl Moments: Coloring Book for Adults and Teens Featuring Cute Cozy Daily Activities for Relaxation
Best Seller

Girl Moments: Coloring Book for Adults and Teens Featuring Cute Cozy Daily Activities for Relaxation

(5,258)
$11.27
FREE delivery Sun, Jun 29 on your first order
Loading...
Samsung Galaxy Tab S10 Ultra Gray 256GB - AI Tablet, 14.6", AMOLED 2X, Anti-Reflection, 420 Nits, Vapor Chamber, S Pen Included (CAD Version & Warranty)

Samsung Galaxy Tab S10 Ultra Gray 256GB - AI Tablet, 14.6", AMOLED 2X, Anti-Reflection, 420 Nits, Vapor Chamber, S Pen Included (CAD Version & Warranty)

(77)
$1,099.99$869.90
FREE delivery Wed, Aug 27
50+ bought in past month
Loading...
Samsung Galaxy Tab A9+ 27.94 cm(11.0 inch) Display, RAM 4 GB, ROM 64 GB Expandable, Wi-Fi, Tablet, (Gray)

Samsung Galaxy Tab A9+ 27.94 cm(11.0 inch) Display, RAM 4 GB, ROM 64 GB Expandable, Wi-Fi, Tablet, (Gray)

(258)
$199.00
FREE delivery Wed, Aug 27
100+ bought in past month
Loading...
Samsung Galaxy Tab A7 10.4" (32GB, 3GB) Ultra-Slim Wi-Fi Only Android 10 One UI Tablet, Snapdragon 662, 7040mAh Battery, SM-T503 (64GB SD Bundle) (Dark Gray)

Samsung Galaxy Tab A7 10.4" (32GB, 3GB) Ultra-Slim Wi-Fi Only Android 10 One UI Tablet, Snapdragon 662, 7040mAh Battery, SM-T503 (64GB SD Bundle) (Dark Gray)

(565)
$149.86
FREE delivery Wed, Aug 27
200+ bought in past month
Loading...
Samsung Galaxy Tab A9+ 64 GB Graphite (CAD Version and Warranty)

Samsung Galaxy Tab A9+ 64 GB Graphite (CAD Version and Warranty)

(1,082)
$209.99$167.99
FREE delivery Wed, Aug 27
500+ bought in past month
Loading...
Magnetic Wireless Charger for iPhone: Fodable 3 in 1 Charging Station for Multiple Apple Devices - Travel Charging Pad Dock for Apple Watch iPhone 16 15 14 13 12 Pro Max Plus & Airpod Global Recycled Standard

Magnetic Wireless Charger for iPhone: Fodable 3 in 1 Charging Station for Multiple Apple Devices - Travel Charging Pad Dock for Apple Watch iPhone 16 15 14 13 12 Pro Max Plus & Airpod Global Recycled Standard

(656)
$49.99
PrimeFREE delivery Sat, Jun 14
100+ bought in past month
Loading...
2 Pack Magnetic Wireless Charger 15W Fast Mag-Safe Charger Stand Compatible with iPhone 16/15/14/13/12 Series Wireless Charging Pad for AirPods 3/2/Pro 2/Pro White Portable Mag Safe Kickstand

2 Pack Magnetic Wireless Charger 15W Fast Mag-Safe Charger Stand Compatible with iPhone 16/15/14/13/12 Series Wireless Charging Pad for AirPods 3/2/Pro 2/Pro White Portable Mag Safe Kickstand

(31)
$24.69
FREE delivery Sat, Jun 14 on your first order
500+ bought in past month
Loading...
Wireless Earbuds, 2025 Bluetooth 5.4 Bass Stereo Headphones with 4 ENC Mic, 48H Playback Ear Buds with Charging Case LED Display,Sport Earphones IPX7 Waterproof for Phone, Android, Pad, White

Wireless Earbuds, 2025 Bluetooth 5.4 Bass Stereo Headphones with 4 ENC Mic, 48H Playback Ear Buds with Charging Case LED Display,Sport Earphones IPX7 Waterproof for Phone, Android, Pad, White

(38)
$27.99
Prime
Loading...
USB C Cable 6ft Type C Charger Cable 3.1A Fast Charging Nylon Braided USB A to USB C Charge Cord for Samsung Galaxy S25 S24 S23 S22 S21 S20 A12 A13 A14 A15 A16 A32 A53 5G Android Phone Tablet PS5 LG

USB C Cable 6ft Type C Charger Cable 3.1A Fast Charging Nylon Braided USB A to USB C Charge Cord for Samsung Galaxy S25 S24 S23 S22 S21 S20 A12 A13 A14 A15 A16 A32 A53 5G Android Phone Tablet PS5 LG

(4,378)
$8.41
FREE delivery Sat, Jun 14 on your first order
400+ bought in past month
Loading...
45W Super Fast Charger USBC Charger, 2Pack 10FT Samsung Charger Fast Charging Cord Cable& Samsung Type C Charger Block for Galaxy S25 S24 S23 S22 S21 Ultra/Plus, Tab S10 S9 S8 S7,iPad,Android Phone

45W Super Fast Charger USBC Charger, 2Pack 10FT Samsung Charger Fast Charging Cord Cable& Samsung Type C Charger Block for Galaxy S25 S24 S23 S22 S21 Ultra/Plus, Tab S10 S9 S8 S7,iPad,Android Phone

(52)
$22.79
FREE delivery Fri, Oct 3 on your first order
1K+ bought in past month
Loading...
Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

Wireless Charger for Samsung - NANAMI 3 in 1 Charging Station for Multiple Devices,Fast Charger Stand Dock for Galaxy S25 S24 S23 Ultra S22 S21 S20 Z Flip Fold,Galaxy Watch 5/5 Pro/4/3,Galaxy Buds Pro

(2,272)
$43.99
PrimeFREE delivery Sat, Jun 14
200+ bought in past month