2689. Extract Kth Character From The Rope Tree
You are given the
rootof a binary tree and an integerk. Besides the left and right children, every node of this tree has two other properties, a stringnode.valcontaining only lowercase English letters (possibly empty) and a non-negative integernode.len. There are two types of nodes in this tree:
- Leaf: These nodes have no children,
node.len = 0, andnode.valis some non-empty string.- Internal: These nodes have at least one child (also at most two children),
node.len > 0, andnode.valis an empty string.The tree described above is called a Rope binary tree. Now we define
S[node]recursively as follows:
- If
nodeis some leaf node,S[node] = node.val,- Otherwise if
nodeis some internal node,S[node] = concat(S[node.left], S[node.right])andS[node].length = node.len.Return k-th character of the string
S[root].Note: If
sandpare two strings,concat(s, p)is a string obtained by concatenatingptos. For example,concat("ab", "zz") = "abzz".
1 | /** |