patternjavaMinor
Print path (leaf to root) with max sum in a binary tree
Viewed 0 times
pathwithleafprintmaxrootbinarysumtree
Problem
I want you to pick my code apart and give me some feedback on how I could make it better or more simple.
Also considering additional storage (
public class MaxSumPath {
private TreeNode root;
private static class TreeNode {
TreeNode left;
TreeNode right;
int item;
TreeNode (TreeNode left, TreeNode right, int item) {
this.left = left;
this.right = right;
this.item = item;
}
}
private static class TreeInfo {
TreeNode node;
int maxCount;
TreeInfo (TreeNode node, int maxCount) {
this.node = node;
this.maxCount = maxCount;
}
}
public void maxSumPath () {
final TreeInfo treeInfo = new TreeInfo(null, 0);
fetchMaxSumPath(root, treeInfo, root.item);
printPath (root, treeInfo);
}
private void fetchMaxSumPath (TreeNode node, TreeInfo treeInfo, int sumSoFar) {
if (node == null) return;
sumSoFar = sumSoFar + node.item;
if (node.right == null && node.left == null) {
if (sumSoFar > treeInfo.maxCount) {
treeInfo.maxCount = sumSoFar;
treeInfo.node = node;
}
return;
}
fetchMaxSumPath(node.left, treeInfo, sumSoFar);
fetchMaxSumPath(node.right, treeInfo, sumSoFar);
}
private boolean printPath (TreeNode node, TreeInfo treeInfo) {
if (node != null && (node == treeInfo.node || printPath(node.left, treeInfo) || printPath(node.right, treeInfo))) {
System.out.print(node.item + ", ");
return true;
}
return false;
}
}Also considering additional storage (
TreeInfo), is it right to say that the space complexity is \$O(1)\$?Solution
You should have a constructor
The
Your
In the
The space complexity is actually O(log n) when you take into account the stack used in recursion. The time complexity is O(n), because you visit every node once when discovering the leaf for the path with the maximum sum, and every node again to find that leaf for printing. You might want to use O(log n) space (which, remember, happens anyway due to the recursion stack) to keep track of the best path while discovering the best sum, which lets you avoid the second pass O(n) pass.
MaxSumPath(TreeNode root). Otherwise, there's no way for any other class to call the code.The
TreeNode(left, right, item) constructor is awkward. Consider rearranging the TreeNode constructor arguments to TreeNode(left, item, right) or TreeNode(item, left, right). I suggest creating a TreeNode(item) constructor as well.Your
TreeInfo could be eliminated by moving its variables into MaxSumPath itself. I'd rename the variables to TreeNode maxLeaf and int maxSum.In the
maxSumPath() method, you call fetchMaxSumPath(root, treeInfo, root.item). It should be fetchMaxSumPath(root, treeInfo, 0) if you don't want to double-count the root node. (Fortunately, this bug happens not to affect the output.)The space complexity is actually O(log n) when you take into account the stack used in recursion. The time complexity is O(n), because you visit every node once when discovering the leaf for the path with the maximum sum, and every node again to find that leaf for printing. You might want to use O(log n) space (which, remember, happens anyway due to the recursion stack) to keep track of the best path while discovering the best sum, which lets you avoid the second pass O(n) pass.
Context
StackExchange Code Review Q#31922, answer score: 2
Revisions (0)
No revisions yet.