Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 927 Bytes

Question_2689.md

File metadata and controls

31 lines (25 loc) · 927 Bytes

LeetCode Records - Question 2689 Extract Kth Character From The Rope Tree

Attempt 1: Use recursion to get the left and right strings

class Solution {
    public char getKthCharacter(RopeTreeNode root, int k) {
        String result = getKthCharacterRecursion(root);

        return result.charAt(k - 1);
    }

    private String getKthCharacterRecursion(RopeTreeNode root) {
        if (root == null) {
            return null;
        } else if (root.len == 0) {
            return root.val;
        }

        String leftString = getKthCharacterRecursion(root.left);
        leftString = leftString != null ? leftString : "";
        String rightString = getKthCharacterRecursion(root.right);
        rightString = rightString != null ? rightString : "";
        
        return leftString + rightString;
    }
}
  • Runtime: 1 ms (Beats: 73.61%)
  • Memory: 52.02 MB (Beats: 56.94%)