Skip to content

Commit 4fac26c

Browse files
Update RecoverTreeFromPreorder.java
1 parent 97a482f commit 4fac26c

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

JAVA/RecoverTreeFromPreorder.java

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Problem: 1028. Recover a Tree From Preorder Traversal
3+
* Link: https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/
4+
*
5+
* You are given a string traversal representing a preorder traversal of a binary tree.
6+
* The string uses dashes '-' to indicate the depth of each node.
7+
* Return the root of the tree that was traversed.
8+
*
9+
* Example:
10+
* Input: traversal = "1-2--3--4-5--6--7"
11+
* Output: Preorder traversal of recovered tree → 1 2 3 4 5 6 7
12+
*
13+
* Approach: Use a stack to maintain the correct parent node at each depth.
14+
*/
15+
16+
17+
18+
/**
19+
* Definition for a binary tree node.
20+
* public class TreeNode {
21+
* int val;
22+
* TreeNode left;
23+
* TreeNode right;
24+
* TreeNode() {}
25+
* TreeNode(int val) { this.val = val; }
26+
* TreeNode(int val, TreeNode left, TreeNode right) {
27+
* this.val = val;
28+
* this.left = left;
29+
* this.right = right;
30+
* }
31+
* }
32+
*/
33+
import java.util.Stack;
34+
class TreeNode {
35+
int val;
36+
TreeNode left, right;
37+
TreeNode(int x) {
38+
val = x;
39+
left = null;
40+
right = null;
41+
}
42+
}
43+
44+
class Solution {
45+
public TreeNode recoverFromPreorder(String traversal) {
46+
Stack<TreeNode> stack = new Stack<>();
47+
int i = 0, n = traversal.length();
48+
49+
while (i < n) {
50+
int depth = 0;
51+
52+
// Count dashes to determine depth
53+
while (i < n && traversal.charAt(i) == '-') {
54+
depth++;
55+
i++;
56+
}
57+
58+
// Read the number (node value)
59+
int numStart = i;
60+
while (i < n && Character.isDigit(traversal.charAt(i))) {
61+
i++;
62+
}
63+
int value = Integer.parseInt(traversal.substring(numStart, i));
64+
65+
TreeNode node = new TreeNode(value);
66+
67+
// If stack size > depth, pop until stack has correct depth parent
68+
while (stack.size() > depth) {
69+
stack.pop();
70+
}
71+
72+
// Attach to parent if exists
73+
if (!stack.isEmpty()) {
74+
TreeNode parent = stack.peek();
75+
if (parent.left == null) {
76+
parent.left = node;
77+
} else {
78+
parent.right = node;
79+
}
80+
}
81+
82+
// Push current node to stack
83+
stack.push(node);
84+
}
85+
86+
// Root node is the first node in stack
87+
while (stack.size() > 1) {
88+
stack.pop();
89+
}
90+
return stack.peek();
91+
}
92+
93+
// Helper function to print tree in preorder
94+
public static void preorder(TreeNode root) {
95+
if (root == null) return;
96+
System.out.print(root.val + " ");
97+
preorder(root.left);
98+
preorder(root.right);
99+
}
100+
101+
public static void main(String[] args) {
102+
Solution sol = new Solution();
103+
String traversal = "1-2--3--4-5--6--7";
104+
TreeNode root = sol.recoverFromPreorder(traversal);
105+
106+
System.out.println("Preorder Traversal of Recovered Tree:");
107+
preorder(root);
108+
}
109+
110+
public boolean divideArray(int[] nums) {
111+
// TODO Auto-generated method stub
112+
throw new UnsupportedOperationException("Unimplemented method 'divideArray'");
113+
}
114+
}

0 commit comments

Comments
 (0)