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