Bea.AI coding blog
  • Tree
    • Tree Traverse
    • Iteration based traverse
    • Typical problems and solutions
      • Max width of binary tree
      • Binary Tree & BST Serialize & Deserialize
      • Lowest common ancestor (LCA) for Binary Tree and BST
      • Subproblem-based Approach for Resolving Max Value on Trees
      • Path sum III
  • Graph
  • Data structure
    • Java data structure
  • Algorithm general templates
    • Tree
    • Trie
    • Graph
  • Java Algorithm Tips and Tricks
    • Java concurrent list
  • Coding patterns & Strategies
  • Coding patterns & template
    • 1. Binary Search
    • 2. Two Pointer
    • 3. Sliding Window
    • 4. Backtracking
    • 5. DFS
    • 6. BFS
    • 7. Stack
    • 8. Heap
    • 9. Prefix Sum
    • 10. Linked List
    • 11. BST
    • 12. Line sweep
    • 14. Tree
    • 15. Graph
    • 16. Bit manipulation
    • 17. Matrix
    • 18. Monotonic Stack
    • 19. Sorting
    • 20. Union Find
    • 21. Trie
    • 22. Dynamic programming
    • 23. Customized Data Structure
  • Code interview steps
  • Leetcode
    • Tree
      • Traversal
      • Construction and Conversion
      • Search and Validation
      • Manipulation and Modification
      • Special Trees
      • Miscellaneous
    • Dynamic programing
      • Classic DP Problems
      • Sequence/Array DP
      • Matrix DP
      • Knapsack Problems
      • String DP
      • Tree/Graph DP
      • Optimization Problems
      • Combinatorial DP
    • DFS
      • Graph Traversal
      • Path Finding
      • Tree Traversal
      • Backtracking
      • Topological Sorting
      • Traversal Order
      • Dynamic Programming with DFS
      • Connected Components
    • BFS
      • Graph
      • Tree
      • Matrix
      • String
      • Others
    • Two points
      • Array
      • String
      • Linked List
      • Sorting
      • Others
    • LinkedList
      • Basic Operations
      • Cycle Detection and Handling
      • Intersection and Union
      • Partitioning and Merging
      • Palindrome
      • Miscellaneous
    • Backtracking
    • Binary Search
    • Union Find
    • Trie
    • Sort
    • Heap
    • Randomness
    • Topological sort
    • Stack
    • HashTable
    • Sliding Window
  • Blind 75
    • Array
    • Binary
    • Dynamic Programming
    • Graph
    • Interval
    • LinkedList
    • Matrix
    • String
    • Tree
    • Heap
  • Companies
    • Apple
Powered by GitBook
On this page
  1. Tree
  2. Typical problems and solutions

Subproblem-based Approach for Resolving Max Value on Trees

PreviousLowest common ancestor (LCA) for Binary Tree and BSTNextPath sum III

Last updated 2 years ago

There are two kinds of max problems on trees based on the approaches, for one type of problems, we usually use the subproblem strategy, while for the other type, we need to use cached DFS or DP.

The typical problems for the first category:

  1. Max depth of the tree:

  2. Diameter of the tree:

  3. Max path sum:

The strategy is similar, we need to transfer the problem to subproblem on left and right children.

Max depth of the tree - node max depth = Math.max(left children depth, right children depth) + 1;

Diameter of the tree - node diameter = left children depth + right children depth;

    int maxDiameter = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        maxDepth(root);
        return maxDiameter;
    }
    private int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftMaxDepth = maxDepth(root.left);
        int rightMaxDepth = maxDepth(root.right);
        maxDiameter = Math.max(maxDiameter, leftMaxDepth + rightMaxDepth);
        return Math.max(leftMaxDepth, rightMaxDepth) + 1;
    }

Max path sum - node single side max path sum = left children single side max path sum + right children single side max path sum

class Solution {
    int maxPathSum = Integer.MIN_VALUE;
  public int maxPathSum(TreeNode root) {
      maxSingleSidePathSum(root);
      return maxPathSum;
  }
  private int maxSingleSidePathSum(TreeNode root) {
      if (root == null) {
          return 0;
      }
      int leftPathSum = Math.max(0, maxSingleSidePathSum(root.left));
      int rightPathSum = Math.max(0, maxSingleSidePathSum(root.right));
      maxPathSum = Math.max(maxPathSum, leftPathSum + rightPathSum + root.val);
      return Math.max(leftPathSum, rightPathSum) + root.val;
  }

}
Maximum Depth of Binary Tree
Diameter of Binary Tree
Binary Tree Maximum Path Sum