Day21-BinaryTree08
669. Trim a Binary Search Tree
Node:
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O()
- Best case (balanced tree): O(log n).
- Worst case (unbalanced tree): O(n).
1 | var trimBST = function(root, low, high) { |
1 | var trimBST = function(root, low, high) { |
108. Convert Sorted Array to Binary Search Tree
Node:
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(logn)
- Call Stack Space: The recursion depth is proportional to the height of the tree. In the best-case scenario, the tree is balanced, and its height is O(log n). Therefore, the recursive calls will consume O(log n) space in the call stack.
- Tree Node Space: Each element in the input array results in a TreeNode, so the total space used by the tree itself is O(n) for the n nodes.
1 | var sortedArrayToBST = function(nums) { |
538. Convert BST to Greater Tree
Node:
- right, mid, left
Time Complexity && Space Complexity
- Time Complexity: O(n)
- Space Complexity: O(n)
1 | var convertBST = function(root) { |
- Title: Day21-BinaryTree08
- Author: Guoyi
- Created at : 2024-10-11 13:07:19
- Updated at : 2024-12-07 03:58:41
- Link: https://guoyiwang.github.io/Algorithm/Day21-BinaryTree08/
- License: This work is licensed under CC BY-NC-SA 4.0.
Comments