Two Sum IV - BST — Hash Set + In-Order Traversal
Find if a BST contains two nodes summing to target using a hash set during DFS traversal.
webcoderspeed.com
20 articles
Find if a BST contains two nodes summing to target using a hash set during DFS traversal.
Convert a sorted linked list to a height-balanced BST by repeatedly finding the middle node as root.
Build a balanced BST from a sorted list in O(n) by counting nodes, building in-order, and consuming list nodes.
Sum all BST values in [low, high] using BST property to prune entire subtrees outside the range.
Find a node with given value in a BST by navigating left or right based on the BST property.
Validate a BST by passing down min and max bounds through DFS, ensuring every node falls strictly within its valid range.
Find the kth smallest element in a BST using inorder traversal (left-root-right) which visits nodes in sorted order.
Find the LCA in a BST in O(h) by navigating left or right based on whether both nodes are in the same subtree.
Find and delete a node from a BST while maintaining BST properties using in-order successor.
Replace each node value with the sum of all values greater than or equal to it using reverse in-order traversal.
Trim a BST so all values lie within [low, high] by recursively pruning out-of-range subtrees.
Serialize a BST to a compact preorder string and reconstruct it using the min-max BST property.
Find and swap the two misplaced nodes in a BST using in-order traversal to detect the violations.
Find the maximum sum of any BST subtree within a binary tree using post-order DFS returning subtree metadata.
Reconstruct a BST from its preorder traversal in O(n) using min-max bounds to place each node.
Implement an in-order BST iterator with O(h) space using a stack to simulate recursive in-order traversal.
Find k values closest to target in a BST using two in-order iterators (forward and reverse) with a two-pointer merge.
Find the inorder successor of a node in a BST when you have access to parent pointers.
Insert a value into a BST by traversing left/right based on comparisons until reaching a null position.
Rearrange a BST into a right-skewed tree with no left children using in-order traversal.