Skip to content

Commit aa91451

Browse files
authored
feat: Binary Tree to String algorithm added (Leetcode 606) (#6727)
* feat: BinaryTreeToString.java Added with tests * fix: javadoc added to BinaryTreeToString * fix: link to leetcode added
1 parent bccaf97 commit aa91451

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.thealgorithms.datastructures.trees;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* Tests for the BinaryTreeToString class.
8+
*/
9+
public class BinaryTreeToStringTest {
10+
11+
@Test
12+
public void testTreeToStringBasic() {
13+
BinaryTree tree = new BinaryTree();
14+
tree.put(1);
15+
tree.put(2);
16+
tree.put(3);
17+
tree.put(4);
18+
19+
BinaryTreeToString converter = new BinaryTreeToString();
20+
String result = converter.tree2str(tree.getRoot());
21+
22+
// Output will depend on insertion logic of BinaryTree.put()
23+
// which is BST-style, so result = "1()(2()(3()(4)))"
24+
Assertions.assertEquals("1()(2()(3()(4)))", result);
25+
}
26+
27+
@Test
28+
public void testSingleNodeTree() {
29+
BinaryTree tree = new BinaryTree();
30+
tree.put(10);
31+
32+
BinaryTreeToString converter = new BinaryTreeToString();
33+
String result = converter.tree2str(tree.getRoot());
34+
35+
Assertions.assertEquals("10", result);
36+
}
37+
38+
@Test
39+
public void testComplexTreeStructure() {
40+
BinaryTree.Node root = new BinaryTree.Node(10);
41+
root.left = new BinaryTree.Node(5);
42+
root.right = new BinaryTree.Node(20);
43+
root.right.left = new BinaryTree.Node(15);
44+
root.right.right = new BinaryTree.Node(25);
45+
46+
BinaryTreeToString converter = new BinaryTreeToString();
47+
String result = converter.tree2str(root);
48+
49+
Assertions.assertEquals("10(5)(20(15)(25))", result);
50+
}
51+
52+
@Test
53+
public void testNullTree() {
54+
BinaryTreeToString converter = new BinaryTreeToString();
55+
Assertions.assertEquals("", converter.tree2str(null));
56+
}
57+
}

0 commit comments

Comments
 (0)