Extract Kth Character From The Rope Tree - Problem

You are given the root of a binary tree and an integer k. Besides the left and right children, every node of this tree has two other properties:

  • A string node.val containing only lowercase English letters (possibly empty)
  • A non-negative integer node.len

There are two types of nodes in this tree:

  • Leaf: These nodes have no children, node.len = 0, and node.val is some non-empty string.
  • Internal: These nodes have at least one child (also at most two children), node.len > 0, and node.val is an empty string.

The tree described above is called a Rope binary tree. Now we define S[node] recursively as follows:

  • If node is some leaf node, S[node] = node.val
  • Otherwise if node is some internal node, S[node] = concat(S[node.left], S[node.right]) and S[node].length = node.len

Return the k-th character of the string S[root].

Note: If s and p are two strings, concat(s, p) is a string obtained by concatenating p to s. For example, concat("ab", "zz") = "abzz".

Input & Output

Example 1 — Basic Rope Tree
$ Input: root = [10,4,"world","hell","o "], k = 6
Output: " "
💡 Note: The rope represents "hello world". The 6th character (1-indexed) is the space character between "hello" and "world".
Example 2 — Simple Leaf
$ Input: root = ["abc"], k = 2
Output: "b"
💡 Note: Single leaf node with "abc". The 2nd character is 'b'.
Example 3 — Larger Tree
$ Input: root = [12,6,"hello","prog","ram"], k = 8
Output: "h"
💡 Note: The rope represents "program" + "hello" = "programhello". The 8th character is 'h' from "hello".

Constraints

  • The sum of node.val.length over all leaf nodes ≤ 104
  • 1 ≤ k ≤ S[root].length
  • node.val contains only lowercase English letters
  • The tree has at most 1000 nodes

Visualization

Tap to expand
Extract Kth Character From Rope Tree INPUT len=10 len=4 "world" "hell" "o " Internal (len>0) Leaf (val) root = [10,4,"world","hell","o "] k = 6 ALGORITHM STEPS 1 Start at Root S[root].len = 10, k = 6 2 Check Left Subtree Left len=4, k=6 > 4 Go right, k = 6 - 4 = 2 3 Reach Leaf "world" Find char at position 2 4 Wait - Recalculate! S = "hell" + "o " + "world" Full String S[root]: h e l l o w o r l 1 2 3 4 5 6 7 DFS: root --{">"} left --{">"} "o " --{">"} char[2] k=6: 6 {"<"}= 4? No --{">"} k=6-4=2 k=2: go left("hell"), 2{"<"}=4? Yes Actual: Navigate to "o "[2] = " " FINAL RESULT The 6th character of S[root]: " " (space) Output: " " OK - Verified! S = "hello world" S[6] = " " (space) DFS Path Taken: root(10) --{">"} left(4) --{">"} right("o ") k=6 --{">"} k=2 --{">"} return "o "[1]=" " Key Insight: Optimized DFS navigates WITHOUT building the full string. At each internal node, compare k with left subtree length: If k {"<"}= left.len, go left. Otherwise, go right with k = k - left.len. This achieves O(h) time where h = tree height, instead of O(n) to build the entire string. The node.len property enables this efficient navigation. TutorialsPoint - Extract Kth Character From The Rope Tree | Optimized DFS Navigation
Asked in
Google 15 Facebook 12 Amazon 8
12.5K Views
Medium Frequency
~15 min Avg. Time
485 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen