[Swift]LeetCode1028. 從先序遍歷還原二叉樹 | Recover a Tree From Preorder Traversal

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-znwekjen-md.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

We run a preorder depth first search on the root of a binary tree.node

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.)git

If a node has only one child, that child is guaranteed to be the left child.github

Given the output S of this traversal, recover the tree and return its root.微信

Example 1:this

Input: "1-2--3--4-5--6--7"
Output: [1,2,5,3,4,6,7] 

Example 2:spa

Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]

Example 3:code

Input: "1-401--349---90--88"
Output: [1,401,null,349,88,90]

Note:htm

  • The number of nodes in the original tree is between 1 and 1000. 
  • Each node will have a value between 1 and 10^9.

咱們從二叉樹的根節點 root 開始進行深度優先搜索。blog

在遍歷中的每一個節點處,咱們輸出 D 條短劃線(其中 D 是該節點的深度),而後輸出該節點的值。(若是節點的深度爲 D,則其直接子節點的深度爲 D + 1。根節點的深度爲 0)。

若是節點只有一個子節點,那麼保證該子節點爲左子節點。

給出遍歷輸出 S,還原樹並返回其根節點 root

示例 1:

輸入:"1-2--3--4-5--6--7"
輸出:[1,2,5,3,4,6,7]

示例 2:

輸入:"1-2--3---4-5--6---7"
輸出:[1,2,5,3,null,6,null,4,null,7]

示例 3:

輸入:"1-401--349---90--88"
輸出:[1,401,null,349,88,90]

提示:

  • 原始樹中的節點數介於 1 和 1000 之間。
  • 每一個節點的值介於 1 和 10 ^ 9 之間。

Runtime: 52 ms
Memory Usage: 20.5 MB
 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     public var val: Int
 5  *     public var left: TreeNode?
 6  *     public var right: TreeNode?
 7  *     public init(_ val: Int) {
 8  *         self.val = val
 9  *         self.left = nil
10  *         self.right = nil
11  *     }
12  * }
13  */
14 class Solution {
15     var len:Int = 0
16     var s:[Character] = [Character]()
17     var pos:Int = 0
18     func recoverFromPreorder(_ S: String) -> TreeNode? {
19         len = S.count
20         s = Array(S)
21         return go(0)
22     }
23     
24     func go(_ dep:Int) -> TreeNode?
25     {
26         var v:Int = 0
27         while(pos < len && s[pos] >= "0" && s[pos] <= "9")
28         {
29             v = v * 10 + (s[pos].ascii - 48)
30             pos += 1
31         }
32         var cur:TreeNode? = TreeNode(v)
33         if hasEdge(dep + 1)
34         {
35             pos += (dep + 1)
36             cur?.left = go(dep + 1)
37         }
38         if hasEdge(dep + 1)
39         {
40             pos += (dep + 1)
41             cur?.right = go(dep + 1)
42         }
43         return cur
44     }
45     
46     func hasEdge(_ d:Int) -> Bool
47     {
48         if pos+d > len-1
49         {
50             return false            
51         }
52         for i in pos..<(pos + d)
53         {
54             if s[i] != "-"
55             {
56                 return false
57             }
58         }
59         return s[pos+d] != "-"
60     }
61 }
62 
63 //Character擴展 
64 extension Character  
65 {  
66   //Character轉ASCII整數值(定義小寫爲整數值)
67    var ascii: Int {
68        get {
69            return Int(self.unicodeScalars.first?.value ?? 0)
70        }       
71     }
72 }
相關文章
相關標籤/搜索