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