diff --git "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" index c7999fe246a86..86bdf0f27ffa7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/README.md" @@ -269,6 +269,36 @@ public class Solution { } ``` +#### Swift + +```swift +class Solution { + private var postorder: [Int] = [] + + func verifyPostorder(_ postorder: [Int]) -> Bool { + self.postorder = postorder + return dfs(0, postorder.count - 1) + } + + private func dfs(_ l: Int, _ r: Int) -> Bool { + if l >= r { + return true + } + let rootValue = postorder[r] + var i = l + while i < r && postorder[i] < rootValue { + i += 1 + } + for j in i.. diff --git "a/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.swift" "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.swift" new file mode 100644 index 0000000000000..a9243b976cab3 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23033. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\345\220\216\345\272\217\351\201\215\345\216\206\345\272\217\345\210\227/Solution.swift" @@ -0,0 +1,25 @@ +class Solution { + private var postorder: [Int] = [] + + func verifyPostorder(_ postorder: [Int]) -> Bool { + self.postorder = postorder + return dfs(0, postorder.count - 1) + } + + private func dfs(_ l: Int, _ r: Int) -> Bool { + if l >= r { + return true + } + let rootValue = postorder[r] + var i = l + while i < r && postorder[i] < rootValue { + i += 1 + } + for j in i..