Skip to content

Commit 5be7d1b

Browse files
committed
fix: javadoc added to BinaryTreeToString
1 parent ef6764c commit 5be7d1b

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

src/main/java/com/thealgorithms/datastructures/trees/BinaryTreeToString.java

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,90 @@
11
package com.thealgorithms.datastructures.trees;
22

33
/**
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>
911
*
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>
1721
*
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
1943
*/
2044
public class BinaryTreeToString {
2145

46+
/** String builder used to accumulate the string representation. */
2247
private StringBuilder sb;
2348

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+
*/
2456
public String tree2str(BinaryTree.Node root) {
2557
if (root == null) {
2658
return "";
2759
}
2860

2961
sb = new StringBuilder();
3062
dfs(root);
31-
// remove the first and last parentheses
63+
64+
// Remove the leading and trailing parentheses added by the root call
3265
return sb.substring(1, sb.length() - 1);
3366
}
3467

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+
*/
3575
private void dfs(BinaryTree.Node node) {
3676
if (node == null) {
3777
return;
3878
}
3979

4080
sb.append("(").append(node.data);
4181

82+
// Recursively build left and right subtrees
4283
if (node.left != null) {
4384
dfs(node.left);
4485
}
4586

87+
// Handle the special case: right child exists but left child is null
4688
if (node.right != null && node.left == null) {
4789
sb.append("()");
4890
dfs(node.right);

0 commit comments

Comments
 (0)