★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rzwsmbqi-hy.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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 #
.node
_9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # #
For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#"
, where #
represents a null node.git
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.github
Each comma separated value in the string must be either an integer or a character '#'
representing null
pointer.算法
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:app
Input: Output: "9,3,4,#,#,1,#,#,2,#,6,#,#"true
Example 2:spa
Input: Output: "1,#"false
Example 3:指針
Input: Output: "9,#,#,1"false
序列化二叉樹的一種方法是使用前序遍歷。當咱們遇到一個非空節點時,咱們能夠記錄下這個節點的值。若是它是一個空節點,咱們能夠使用一個標記值記錄,例如 #
。code
_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 }