|
| 1 | +package com.thealgorithms.datastructures.trees; |
| 2 | + |
| 3 | +/** |
| 4 | + * Leetcode 606: Construct String from Binary Tree: |
| 5 | + * https://leetcode.com/problems/construct-string-from-binary-tree/ |
| 6 | + * |
| 7 | + * Utility class to convert a {@link BinaryTree} into its string representation. |
| 8 | + * <p> |
| 9 | + * The conversion follows a preorder traversal pattern (root → left → right) |
| 10 | + * and uses parentheses to denote the tree structure. |
| 11 | + * Empty parentheses "()" are used to explicitly represent missing left children |
| 12 | + * when a right child exists, ensuring the structure is unambiguous. |
| 13 | + * </p> |
| 14 | + * |
| 15 | + * <h2>Rules:</h2> |
| 16 | + * <ul> |
| 17 | + * <li>Each node is represented as {@code (value)}.</li> |
| 18 | + * <li>If a node has only a right child, include {@code ()} before the right |
| 19 | + * child |
| 20 | + * to indicate the missing left child.</li> |
| 21 | + * <li>If a node has no children, it appears as just {@code (value)}.</li> |
| 22 | + * <li>The outermost parentheses are removed from the final string.</li> |
| 23 | + * </ul> |
| 24 | + * |
| 25 | + * <h3>Example:</h3> |
| 26 | + * |
| 27 | + * <pre> |
| 28 | + * Input tree: |
| 29 | + * 1 |
| 30 | + * / \ |
| 31 | + * 2 3 |
| 32 | + * \ |
| 33 | + * 4 |
| 34 | + * |
| 35 | + * Output string: |
| 36 | + * "1(2()(4))(3)" |
| 37 | + * </pre> |
| 38 | + * |
| 39 | + * <p> |
| 40 | + * This implementation matches the logic from LeetCode problem 606: |
| 41 | + * <i>Construct String from Binary Tree</i>. |
| 42 | + * </p> |
| 43 | + * |
| 44 | + * @author Muhammad Junaid |
| 45 | + * @see BinaryTree |
| 46 | + */ |
| 47 | +public class BinaryTreeToString { |
| 48 | + |
| 49 | + /** String builder used to accumulate the string representation. */ |
| 50 | + private StringBuilder sb; |
| 51 | + |
| 52 | + /** |
| 53 | + * Converts a binary tree (given its root node) to its string representation. |
| 54 | + * |
| 55 | + * @param root the root node of the binary tree |
| 56 | + * @return the string representation of the binary tree, or an empty string if |
| 57 | + * the tree is null |
| 58 | + */ |
| 59 | + public String tree2str(BinaryTree.Node root) { |
| 60 | + if (root == null) { |
| 61 | + return ""; |
| 62 | + } |
| 63 | + |
| 64 | + sb = new StringBuilder(); |
| 65 | + dfs(root); |
| 66 | + |
| 67 | + // Remove the leading and trailing parentheses added by the root call |
| 68 | + return sb.substring(1, sb.length() - 1); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Performs a recursive depth-first traversal to build the string. |
| 73 | + * Each recursive call appends the node value and its children (if any) |
| 74 | + * enclosed in parentheses. |
| 75 | + * |
| 76 | + * @param node the current node being processed |
| 77 | + */ |
| 78 | + private void dfs(BinaryTree.Node node) { |
| 79 | + if (node == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + sb.append("(").append(node.data); |
| 84 | + |
| 85 | + // Recursively build left and right subtrees |
| 86 | + if (node.left != null) { |
| 87 | + dfs(node.left); |
| 88 | + } |
| 89 | + |
| 90 | + // Handle the special case: right child exists but left child is null |
| 91 | + if (node.right != null && node.left == null) { |
| 92 | + sb.append("()"); |
| 93 | + dfs(node.right); |
| 94 | + } else if (node.right != null) { |
| 95 | + dfs(node.right); |
| 96 | + } |
| 97 | + |
| 98 | + sb.append(")"); |
| 99 | + } |
| 100 | +} |
0 commit comments