Skip to main content

Posts

Leetcode 98: Validate Binary Search Tree

  1. Problem Summary You’re given the root of a binary tree and must determine if it is a  valid Binary Search Tree (BST) . A binary tree is a valid BST if for every node: All values in the left subtree are  strictly less  than the node’s value. All values in the right subtree are  strictly greater  than the node’s value. Both left and right subtrees are themselves valid BSTs. Input:  root (binary tree root, not null; up to 10 4  nodes) Output:  Boolean indicating whether the tree is a valid BST. Node values can be as low as -2 31  and as high as 2 31 - 1. 2. Examples Explanation Example 1: Input:  root = [2,1,3] Tree: 2 left → 1 right → 3 Check: Left subtree of 2 has 1 < 2. Right subtree of 2 has 3 > 2. Subtrees rooted at 1 and 3 are leaves, so valid. Result: true. Example 2: Input:  root = [5,1,4,null,null,3,6] Tree: 5 left → 1 ri...

Leetcode 97: Interleaving String

  1. Problem Summary You’re given three strings s1, s2, and s3. Determine whether s3 can be formed by  interleaving all characters of s1 and s2 , preserving the relative order of characters from each string. You  cannot  reorder characters inside s1 or s2. At each step, you choose the next char either from s1 or from s2. All characters of s1 and s2 must be used exactly once. Formally, s3 is an interleaving of s1 and s2 if: len(s3) = len(s1) + len(s2), and There exists a merge of s1 and s2, preserving internal order of each, that equals s3. Input: s1, s2 with length up to 100 s3 with length up to 200 Output: Boolean: true if s3 is an interleaving of s1 and s2, else false. The “substring splitting” notation in the statement boils down to this standard definition: merge s1...