PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep
|Home/Coding & Algorithms/TikTok

Compute maximum path sum in a binary tree

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of binary tree data structures and algorithmic competency for computing path-based aggregates, encompassing recursion, tree traversal, and handling of positive and negative node values.

  • hard
  • TikTok
  • Coding & Algorithms
  • Software Engineer

Compute maximum path sum in a binary tree

Company: TikTok

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

You are given the root of a binary tree where each node contains an integer value (which may be positive, zero, or negative). A **path** in the tree is defined as any sequence of nodes such that: - Each pair of consecutive nodes in the sequence is connected by an edge in the tree. - A path may start and end at any nodes in the tree. - A path must contain at least one node. - The path cannot reuse the same node more than once (i.e., it is a simple path; it does not branch or form cycles). The **path sum** is the sum of the node values along that path. ### Task Implement a function that returns the **maximum path sum** over all possible paths in the tree. ### Function Signature You can assume a typical binary tree node definition, for example in a C++/Java-like style: ```text class TreeNode { int val; TreeNode left; TreeNode right; } ``` Write a function: ```text int maxPathSum(TreeNode root) ``` which returns the maximum path sum. ### Constraints - The number of nodes in the tree is in the range [1, 3 * 10^4]. - Each node value is in the range [-10^3, 10^3]. - The tree is not necessarily balanced. ### Examples **Example 1** Input tree (level-order representation): `[1, 2, 3]` This corresponds to the structure: ```text 1 / \ 2 3 ``` All possible simple paths and their sums: - 2 → 1 → 3 has sum 2 + 1 + 3 = 6 - 2 → 1 has sum 3 - 1 → 3 has sum 4 - Single-node paths: 2, 1, 3 Output: `6` **Example 2** Input tree (level-order): `[-10, 9, 20, null, null, 15, 7]` Structure: ```text -10 / \ 9 20 / \ 15 7 ``` The maximum path is 15 → 20 → 7 with sum 15 + 20 + 7 = 42. Output: `42` **Example 3** Input tree (single node): `[-3]` Only one possible path: the single node itself. Output: `-3` ### Notes - The maximum path is allowed to start and end at any two nodes in the tree. - The path is not required to pass through the root.

Quick Answer: This question evaluates understanding of binary tree data structures and algorithmic competency for computing path-based aggregates, encompassing recursion, tree traversal, and handling of positive and negative node values.

Related Interview Questions

  • Parse a nested list from a string - TikTok (medium)
  • Implement stacks, streaming median, and upward path sum - TikTok (easy)
  • Maximize sum with no adjacent elements - TikTok (medium)
  • Implement stack variants and path-sum check - TikTok (medium)
  • Find the longest palindromic substring - TikTok (easy)
TikTok logo
TikTok
Oct 3, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
3
0

You are given the root of a binary tree where each node contains an integer value (which may be positive, zero, or negative).

A path in the tree is defined as any sequence of nodes such that:

  • Each pair of consecutive nodes in the sequence is connected by an edge in the tree.
  • A path may start and end at any nodes in the tree.
  • A path must contain at least one node.
  • The path cannot reuse the same node more than once (i.e., it is a simple path; it does not branch or form cycles).

The path sum is the sum of the node values along that path.

Task

Implement a function that returns the maximum path sum over all possible paths in the tree.

Function Signature

You can assume a typical binary tree node definition, for example in a C++/Java-like style:

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
}

Write a function:

int maxPathSum(TreeNode root)

which returns the maximum path sum.

Constraints

  • The number of nodes in the tree is in the range [1, 3 * 10^4].
  • Each node value is in the range [-10^3, 10^3].
  • The tree is not necessarily balanced.

Examples

Example 1

Input tree (level-order representation): [1, 2, 3]

This corresponds to the structure:

    1
   / \
  2   3

All possible simple paths and their sums:

  • 2 → 1 → 3 has sum 2 + 1 + 3 = 6
  • 2 → 1 has sum 3
  • 1 → 3 has sum 4
  • Single-node paths: 2, 1, 3

Output: 6

Example 2

Input tree (level-order): [-10, 9, 20, null, null, 15, 7]

Structure:

     -10
     /  \
    9    20
        /  \
       15   7

The maximum path is 15 → 20 → 7 with sum 15 + 20 + 7 = 42.

Output: 42

Example 3

Input tree (single node): [-3]

Only one possible path: the single node itself.

Output: -3

Notes

  • The maximum path is allowed to start and end at any two nodes in the tree.
  • The path is not required to pass through the root.

Submit Your Answer

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More TikTok•More Software Engineer•TikTok Software Engineer•TikTok Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.