| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | using LeetCode.Core.Models; |
| | 13 | |
|
| | 14 | | namespace LeetCode.Algorithms.RecoverTreeFromPreorderTraversal; |
| | 15 | |
|
| | 16 | | /// <inheritdoc /> |
| | 17 | | public class RecoverTreeFromPreorderTraversalDepthFirstSearch : IRecoverTreeFromPreorderTraversal |
| | 18 | | { |
| | 19 | | /// <summary> |
| | 20 | | /// Time complexity - O(n) |
| | 21 | | /// Space complexity - O(n) |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="traversal"></param> |
| | 24 | | /// <returns></returns> |
| | 25 | | public TreeNode RecoverFromPreorder(string traversal) |
| 3 | 26 | | { |
| 3 | 27 | | var index = 0; |
| | 28 | |
|
| 3 | 29 | | return Build(traversal, 0, ref index)!; |
| 3 | 30 | | } |
| | 31 | |
|
| | 32 | | private static TreeNode? Build(string traversal, int currentDepth, ref int index) |
| 41 | 33 | | { |
| 41 | 34 | | if (index >= traversal.Length) |
| 9 | 35 | | { |
| 9 | 36 | | return null; |
| | 37 | | } |
| | 38 | |
|
| 32 | 39 | | var depth = GetDepth(traversal, ref index); |
| | 40 | |
|
| 32 | 41 | | if (depth != currentDepth) |
| 13 | 42 | | { |
| 13 | 43 | | index -= depth; |
| | 44 | |
|
| 13 | 45 | | return null; |
| | 46 | | } |
| | 47 | |
|
| 19 | 48 | | var value = GetValue(traversal, ref index); |
| 19 | 49 | | var left = Build(traversal, currentDepth + 1, ref index); |
| 19 | 50 | | var right = Build(traversal, currentDepth + 1, ref index); |
| | 51 | |
|
| 19 | 52 | | return new TreeNode(value, left, right); |
| 41 | 53 | | } |
| | 54 | |
|
| | 55 | | private static int GetValue(string traversal, ref int index) |
| 19 | 56 | | { |
| 19 | 57 | | var value = 0; |
| | 58 | |
|
| 44 | 59 | | while (index < traversal.Length && char.IsDigit(traversal[index])) |
| 25 | 60 | | { |
| 25 | 61 | | value *= 10; |
| 25 | 62 | | value += traversal[index] - '0'; |
| | 63 | |
|
| 25 | 64 | | index++; |
| 25 | 65 | | } |
| | 66 | |
|
| 19 | 67 | | return value; |
| 19 | 68 | | } |
| | 69 | |
|
| | 70 | | private static int GetDepth(string traversal, ref int index) |
| 32 | 71 | | { |
| 32 | 72 | | var depth = 0; |
| | 73 | |
|
| 82 | 74 | | while (index < traversal.Length && traversal[index] == '-') |
| 50 | 75 | | { |
| 50 | 76 | | depth++; |
| 50 | 77 | | index++; |
| 50 | 78 | | } |
| | 79 | |
|
| 32 | 80 | | return depth; |
| 32 | 81 | | } |
| | 82 | | } |