Day18-BinaryTree06
530. Minimum Absolute Difference in BST
Node:
- Find the max and min of every node
- Calculate the absolue diff for every node by the min and max
res = Math.min(res, rootVal - min, max - rootVal);
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
1 | var getMinimumDifference = function (root) { |
501. Find Mode in Binary Search Tree
Node:
- Since there is a BST and the left subtree less than or equal to the root, the right subtree greater than or equal to the node’s key.
- We need use left, mid, right inorder to check the duplicate the node
- Use maxCount and preNode to count to reduce the Space Complexity to 1(Assume that the implicit stack space incurred due to recursion does not count)
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
1 | var findMode = function(root) { |
236. Lowest Common Ancestor of a Binary Tree
Node:
- Postorder: left, right, mid
- Need know the left or right first
- If left or right not include p or q, root will be null
- If left or right, one is p or q, return this one
- If left is p, right is q, return the root
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
1 | var lowestCommonAncestor = function(root, p, q) { |
- Title: Day18-BinaryTree06
- Author: Guoyi
- Created at : 2024-10-11 13:06:46
- Updated at : 2024-12-07 03:58:41
- Link: https://guoyiwang.github.io/Algorithm/Day18-BinaryTree06/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments