Find the maximum path sum between two leaves of a binary tree using JavaScript
Last Updated :
21 Aug, 2024
Given the root of a binary tree, return the maximum path sum of any non-empty path. A path in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence at most once. Note that the path does not need to pass through the root. The path sum of a path is the sum of the node's values in the path.
Maximum path sum is 42.Using Postorder Traversal
In this approach we traverse each node of the tree using inorder, preorder, or postorder traversal. For each node, consider it as the root and recursively find the maximum left path sum and maximum right path sum. Update the final answer with the maximum value found.
Example: To demonstrate finding the maximum path sum between to leaves of a binary tree using Javascript.
JavaScript
class TreeNode {
constructor(val) {
this.val = val;
this.left = this.right = null;
}
}
// Helper function to find the maximum
// path sum from the given node to any leaf
function PathSum(node)
{
if (node === null) return 0;
if (node.left === null && node.right === null)
return node.val;
// Leaf node
const leftSum = node
.left ? PathSum(node.left) : -Infinity;
const rightSum = node
.right ? PathSum(node.right) : -Infinity;
return node
.val + Math
.max(leftSum, rightSum);
}
// Function to find the maximum path sum between
// two leaf nodes considering the given node as root
function maxPathSumNode(node)
{
if (node === null) return -Infinity;
if (node.left === null || node.right === null)
return -Infinity;
const leftSum = PathSum(node.left);
const rightSum = PathSum(node.right);
return leftSum + rightSum + node.val;
}
// Function to traverse the tree
// and update the maximum path sum
function traverseTree(node, maxSum) {
if (node === null) return;
const currentMax = maxPathSumNode(node);
if (currentMax > maxSum.value) maxSum.
value = currentMax;
traverseTree(node.left, maxSum);
traverseTree(node.right, maxSum);
}
// The main function to return
// the maximum sum between two leaves
function maxSum(root) {
let maxSum = { value: -Infinity };
traverseTree(root, maxSum);
return maxSum.value;
}
// Utility function to create a new tree node
function newNode(data) {
return new TreeNode(data);
}
// Example usage
const root = newNode(-10);
root.left = newNode(9);
root.right = newNode(20);
root.right.left = newNode(15);
root.right.right = newNode(7);
console.log("Max path sum of the given binary tree is " + maxSum(root));
Time Complexity: O(N^2) in the worst case due to repeated subtree traversals.
Space Complexity: O(H), where H is the height of the tree (O(log N) for a balanced tree and O(N) for a skewed tree).
Using optimized DFS( Depth first search)
In this approach we start with the base case. If the current node is null, return 0. For each node, calculate the maximum path sum that includes that node. Recursively traverse the left and right subtrees to get their maximum path sums. Update the global maximum sum if a new maximum path sum is found. Return the maximum path sum including the current node to the parent.
Example: To demonstrate finding the maximum path sum between to leaves of a binary tree using Javascript.
JavaScript
class TreeNode {
constructor(val = 0, left = null, right = null)
{
this.val = val;
this.left = left;
this.right = right;
}
}
class Solution {
maxPathSum(root) {
let ans = [-Infinity];
// Initialize with negative
// infinity for comparison
function DFS(root)
{
if (root === null)
{
return 0;
}
let lmax = Math
.max(0, DFS(root.left));
let rmax = Math
.max(0, DFS(root.right));
// Update the overall
// maximum path sum
ans[0] = Math
.max(ans[0], root.val + lmax + rmax);
// Return the maximum path sum
// that starts from the current node
return root
.val + Math
.max(lmax, rmax);
}
// Call the DFS function
// on the root node
DFS(root);
// Return the final maximum path sum
return ans[0] !== -Infinity ? ans[0] : 0;
// If ans is still -Infinity, return 0
}
}
// Example input
let root = new TreeNode(-10);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
let solution = new Solution();
console.log(solution.maxPathSum(root));
Time Complexity: O(N)
Space Complexity: O(H), where H is the height of the tree (O(log N) for a balanced tree and O(N) for a skewed tree).
Using Dynamic Programming on Trees
This approach involves the use of dynamic programming (DP) to avoid redundant calculations. We maintain a DP table where each entry corresponds to the maximum path sum obtainable for the subtree rooted at each node. By caching the results of the subproblems, we can reduce the time complexity of the solution.
Steps:
- For each node, calculate the maximum path sum that can be obtained starting from that node (this can be considered as the subproblem).
- Use the results of the subproblems to calculate the maximum path sum that includes the node and its children.
- Update the global maximum path sum if a new maximum is found.
Example: Implementation of above discussed approach
JavaScript
class TreeNode {
constructor(val) {
this.val = val;
this.left = this.right = null;
}
}
class Solution {
constructor() {
this.dp = new Map(); // Map to store the DP results
}
maxPathSum(root) {
let maxSum = { value: -Infinity };
this.findMaxPath(root, maxSum);
return maxSum.value;
}
findMaxPath(node, maxSum) {
if (node === null) return 0;
// Check if the result for this node is already calculated
if (this.dp.has(node)) return this.dp.get(node);
// Calculate the maximum path sum of left and right children
let leftMax = Math.max(0, this.findMaxPath(node.left, maxSum));
let rightMax = Math.max(0, this.findMaxPath(node.right, maxSum));
// Update the global maximum path sum including this node
let currentMax = node.val + leftMax + rightMax;
maxSum.value = Math.max(maxSum.value, currentMax);
// Store the result in the DP table and return the maximum path sum
// starting from this node to its parent
let result = node.val + Math.max(leftMax, rightMax);
this.dp.set(node, result);
return result;
}
}
// Example usage
let root = new TreeNode(-10);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
let solution = new Solution();
console.log("Max path sum of the given binary tree is " + solution.maxPathSum(root));
OutputMax path sum of the given binary tree is 42
Similar Reads
Find the Sum of All Left Leaves in a Given Binary Tree using JavaScript
Given a Binary Tree, find the sum of all left leaves in it. A binary tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. Finding the sum of all left leaves involves traversing the entire binary tree and checking each n
4 min read
Find the Distance Between Two Nodes in the Binary Tree using JavaScript
Given two nodes, our task is to find the distance between two nodes in a binary tree using JavaScript, no parent pointers are given. The distance between two nodes is the minimum number of edges to be traversed to reach one node from another. Examples: Input: Binary Tree as described above in diagra
5 min read
JavaScript Program to Find Maximum Width of Binary Tree
The maximum width of a given binary tree can be calculated by finding the maximum of all the level widths. The maximum width of a Binary tree is the maximum number of nodes present at any level. The maximum width of the Binary tree will include only the present node and not include null nodes. The m
4 min read
Difference Between Sums of Odd Level and Even Level Nodes of a Binary Tree using JavaScript
Given a Binary Tree, our task is to find the difference between the sum of nodes at odd level and the sum of nodes at even level using JavaScript. Example: Input: 15 / \ 12 16 / \ \ 11 14 18 / / \ 13 17 19 Output: -19Explanation: In the above tree, sum of nodes at odd level is (15 + 11 + 14 + 18)whi
4 min read
Find the Preorder Successor of a Given Node in a Binary Tree using JavaScript
The Preorder successor of a given node is a node that occurs after the given node in the preorder traversal of the binary tree. The preorder traversal is a traversal in which the root node is visited first, then the left child, and then the right child. Example: The pre-order successor of a binary t
2 min read
Count the Number of Nodes in a Complete Binary tree using JavaScript
We need to count the number of the nodes in the complete binary tree using JavaScript. The complete binary tree is a binary tree in which every level then except possibly the last, is completely filled and all the nodes are as far left as possible. There are several approaches for counting the numbe
4 min read
Merge Two Balanced Binary Search Trees using JavaScript
We are given two different Balanced Binary Search Trees and we have to merge them to form a single balanced binary search tree. A Balanced Binary Search Tree (BST) is a binary search tree that keeps its height close to the minimum possible height. This is achieved by ensuring that the height differe
4 min read
Convert a Binary Tree to a Binary Search Tree using JavaScript
Given a Binary Tree, the task is to convert it to a Binary Search Tree. The conversion must be done in such a way that it keeps the original structure of the Binary Tree. Example: To demonstrate the conversion of the Binary Tree to the Binary Search Tree. Input: 10 / \ 2 7 / \ 8 4Output: 8 / \ 4 10
2 min read
Print the nodes having exactly one child in a Binary tree using JavaScript
Given a binary tree, our task is to return the number of nodes in the Binary tree that have at least one child otherwise return â-1â if no such node exists. Examples: Input: 1 / \ 2 3 / \ 4 5 / 6Output: 3Explanation:There are three nodes in the Binary tree that have at least one child that are 1,2,4
4 min read
Boundary Traversal of Binary Tree using JavaScript
The Boundary Traversal of the Binary Tree can be done by traversing the left, right, and leaf parts of the Binary Tree. We will break the Boundary Traversal of Binary Tree using JavaScript in three parts. First, we will traverse the left part, right part, and leaf parts of the Binary Tree and print
4 min read