LeetCode 1028. Recover a Tree From Preorder Traversal
原题链接在这里:https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/description/
题目:
We run a preorder depth-first search (DFS) on the root
of a binary tree.
At each node in this traversal, we output D
dashes (where D
is the depth of this node), then we output the value of this node. If the depth of a node is D
, the depth of its immediate child is D + 1
. The depth of the root
node is 0
.
If a node has only one child, that child is guaranteed to be the left child.
Given the output traversal
of this traversal, recover the tree and return its root
.
Example 1:
Input: traversal = "1-2--3--4-5--6--7" Output: [1,2,5,3,4,6,7]
Example 2:
Input: traversal = "1-2--3---4-5--6---7" Output: [1,2,5,3,null,6,null,4,null,7]
Example 3:
Input: traversal = "1-401--349---90--88" Output: [1,401,null,349,88,90]
Constraints:
- The number of nodes in the original tree is in the range
[1, 1000]
. 1 <= Node.val <= 109
题解:
For each number, the count of - before it means which level it belongs to.
Using a stack to maintain the trace of preorder visited node.
The first number is put into stack.
Then when we have -2, we know it is the child of root. The level is 1.
We keep popping until we have 1 node in the stack and assign 2 to its left or right child. If it doesn't have left child, assign to left. If not, assign to right.
In the end, the lasrt node in the stack is the root.
Time Complexity: O(traversal.length()).
Space: O(h). h is the height of the tree.
AC Java:
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode() {} 8 * TreeNode(int val) { this.val = val; } 9 * TreeNode(int val, TreeNode left, TreeNode right) { 10 * this.val = val; 11 * this.left = left; 12 * this.right = right; 13 * } 14 * } 15 */ 16 class Solution { 17 public TreeNode recoverFromPreorder(String traversal) { 18 Stack<TreeNode> stk = new Stack<>(); 19 int i = 0; 20 while(i < traversal.length()){ 21 int level = 0; 22 while(i < traversal.length() && traversal.charAt(i) == '-'){ 23 level++; 24 i++; 25 } 26 27 int val = 0; 28 while(i < traversal.length() && traversal.charAt(i) != '-'){ 29 val = val * 10 + (traversal.charAt(i) - '0'); 30 i++; 31 } 32 33 TreeNode node = new TreeNode(val); 34 while(stk.size() > level){ 35 stk.pop(); 36 } 37 38 if(!stk.isEmpty()){ 39 TreeNode top = stk.peek(); 40 if(top.left == null){ 41 top.left = node; 42 }else{ 43 top.right = node; 44 } 45 } 46 47 stk.push(node); 48 } 49 50 while(stk.size() > 1){ 51 stk.pop(); 52 } 53 54 return stk.pop(); 55 } 56 }
AC Python:
1 # Definition for a binary tree node. 2 # class TreeNode: 3 # def __init__(self, val=0, left=None, right=None): 4 # self.val = val 5 # self.left = left 6 # self.right = right 7 class Solution: 8 def recoverFromPreorder(self, traversal: str) -> Optional[TreeNode]: 9 stk = [] 10 i = 0 11 while i < len(traversal): 12 level = 0 13 while i < len(traversal) and traversal[i] == '-': 14 level += 1 15 i += 1 16 val = 0 17 while i < len(traversal) and traversal[i] != '-': 18 val = val * 10 + int(traversal[i]) 19 i += 1 20 21 while len(stk) > level: 22 stk.pop() 23 24 node = TreeNode(val) 25 if stk: 26 if not stk[-1].left: 27 stk[-1].left = node 28 else: 29 stk[-1].right = node 30 stk.append(node) 31 32 return stk[0] 33
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步