[Swift]LeetCode331. 驗證二叉樹的前序序列化 | Verify Preorder Serialization of a Binary Tree
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node‘s value. If it is a null node, we record using a sentinel value such as #
.
_9_ / 3 2 / \ / 4 1 # 6 / \ / \ / # # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#"
#
represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character ‘#‘
representing null
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3"
.
Example 1:
Input:"9,3,4,#,#,1,#,#,2,#,6,#,#"
Output:true
Example 2:
Input:"1,#"
Output:false
Example 3:
Input:"9,#,#,1"
Output:false
序列化二叉樹的一種方法是使用前序遍歷。當我們遇到一個非空節點時,我們可以記錄下這個節點的值。如果它是一個空節點,我們可以使用一個標記值記錄,例如 #
_9_ / 3 2 / \ / 4 1 # 6 / \ / \ / # # # # # #
例如,上面的二叉樹可以被序列化為字符串 "9,3,4,#,#,1,#,#,2,#,6,#,#"
,其中 #
代表一個空節點。
給定一串以逗號分隔的序列,驗證它是否是正確的二叉樹的前序序列化。編寫一個在不重構樹的條件下的可行算法。
每個以逗號分隔的字符或為一個整數或為一個表示 null
指針的 ‘#‘
。
你可以認為輸入格式總是有效的,例如它永遠不會包含兩個連續的逗號,比如 "1,,3"
。
示例 1:
輸入:"9,3,4,#,#,1,#,#,2,#,6,#,#"
輸出:true
示例 2:
輸入:"1,#"
輸出:false
示例 3:
輸入:"9,#,#,1"
輸出:false
72ms
1 class Solution { 2 func isValidSerialization(_ preorder: String) -> Bool { 3 let set = CharacterSet(charactersIn: ",") 4 let preorderArr = preorder.components(separatedBy: set) 5 6 if preorderArr.count % 2 == 0 { 7 return false 8 } 9 10 var i = 1 11 12 for c in preorderArr { 13 if i == 0 { 14 return false 15 } 16 if c == "#" { 17 i -= 1 18 }else { 19 i += 1 20 } 21 } 22 23 return i == 0 24 } 25 }
76ms
1 class Solution { 2 func isValidSerialization(_ preorder: String) -> Bool { 3 let arr = preorder.components(separatedBy:",") 4 if arr[0] == "#" && arr.count != 1 { 5 return false 6 } 7 var count = 0 8 9 for i in 0..<arr.count { 10 let str = arr[i] 11 12 if str == "#" { 13 count += 1 14 } else { 15 count -= 1 16 } 17 18 if count == 1 && i != arr.count - 1 { 19 return false 20 } 21 } 22 23 return count == 1 24 } 25 }
80ms
1 class Solution { 2 func isValidSerialization(_ preorder: String) -> Bool { 3 let nodes = preorder.components(separatedBy: ",") 4 var stack = [String]() 5 6 for i in 0..<nodes.count { 7 let node = nodes[i] 8 while node == "#" && !stack.isEmpty && stack.last! == "#" { 9 stack.removeLast() 10 if stack.isEmpty { 11 return false 12 } 13 stack.removeLast() 14 } 15 stack.append(node) 16 } 17 18 return stack.count == 1 && stack.last! == "#" 19 } 20 }
140ms
1 class Solution { 2 func isValidSerialization(_ preorder: String) -> Bool { 3 let res = preorder.components(separatedBy: ",") 4 var diff = 1 5 6 for r in res { 7 diff -= 1 8 if diff < 0 { 9 return false 10 } 11 if r != "#" { 12 diff += 2 13 } 14 } 15 16 return diff == 0 17 } 18 }
[Swift]LeetCode331. 驗證二叉樹的前序序列化 | Verify Preorder Serialization of a Binary Tree