3
3
4
4
// Define the TreeNode class to represent nodes in the binary tree.
5
5
class TreeNode {
6
- int val ; // Value stored in the node
7
- TreeNode left ; // Pointer to the left child
8
- TreeNode right ; // Pointer to the right child
6
+ int val ;// Value stored in the node
7
+ TreeNode left ;// Pointer to the left child
8
+ TreeNode right ;// Pointer to the right child
9
9
10
10
// Constructor to create a new node with a given value
11
11
TreeNode (int val ) {
12
12
this .val = val ;
13
- this .left = null ; // Initially, the left child is null
13
+ this .left = null ; // Initially, the left child is null
14
14
this .right = null ; // Initially, the right child is null
15
15
}
16
16
}
@@ -115,11 +115,11 @@ public static void main(String[] args) {
115
115
// / \
116
116
// 4 5
117
117
118
- TreeNode root = new TreeNode (1 ); // Create the root node with value 1
119
- root .left = new TreeNode (2 ); // Create the left child (value 2)
120
- root .right = new TreeNode (3 ); // Create the right child (value 3)
121
- root .right .left = new TreeNode (4 ); // Create the left child of node 3 (value 4)
122
- root .right .right = new TreeNode (5 ); // Create the right child of node 3 (value 5)
118
+ TreeNode root = new TreeNode (1 ); // Create the root node with value 1
119
+ root .left = new TreeNode (2 ); // Create the left child (value 2)
120
+ root .right = new TreeNode (3 ); // Create the right child (value 3)
121
+ root .right .left = new TreeNode (4 ); // Create the left child of node 3 (value 4)
122
+ root .right .right = new TreeNode (5 ); // Create the right child of node 3 (value 5)
123
123
124
124
// Instantiate the SerializeaBinaryTree class
125
125
SerializeaBinaryTree serializer = new SerializeaBinaryTree ();
@@ -132,7 +132,6 @@ public static void main(String[] args) {
132
132
// Deserialize the string back to a binary tree
133
133
TreeNode deserializedRoot = serializer .deserialize (serializedTree );
134
134
System .out .println ("Tree deserialized successfully. Root value: " + deserializedRoot .val );
135
- // Expected output: Root value should be 1
136
135
}
137
136
}
138
137
0 commit comments