Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warmup : validate BST

Swift Warmup : validate BST

Johnlin

March 07, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. 98. Validate Binary Search Tree • https://leetcode.com/problems/validate-binary- search-tree/ • Given

    a binary tree, determine if it is a valid binary search tree (BST). • څ㟬Ұݸೋݩथɼ൑ᏗሏੋෆੋҰݸ༗Ꮘతೋݩ፺ਘ थɻ
  2. • Assume a BST is defined as follows:
 ೋݩ፺ਘथఆٛ೗Լɿ •

    The left subtree of a node contains only nodes with keys less than the node's key.
 ࠨࢠथཫతઅᴍతᆴ౎ൺຊ਎తᆴؐখɻ • The right subtree of a node contains only nodes with keys greater than the node's key.
 ӈࢠथཫతઅᴍతᆴ౎ൺຊ਎తᆴؐେɻ • Both the left and right subtrees must also be binary search trees.
 ࠨࢠथ࿨ӈࢠथ౎ੋೋݩ፺ਘथɻ
  3. ൣྫ • 2
 / \
 1 3 • 1 <

    2 ׌ 3 > 2 ɼ߹๏తೋݩ፺ਘथɻ
  4. ൣྫ • 1
 / \
 2 3 • 2 >

    1 ɼ ෆ߹๏తೋݩ፺ਘथɻ
  5. ൣྫ • ୈҰ૚
 3<8<10ɼ ߹๏ • ୈೋ૚
 1<3<6 ׌ 10

    < 14ɼ߹๏ • ୈࡾ૚
 4<6<7 ׌ 13<14ɼ߹๏
  6. ᒾҰҰݸࢠथੋෆੋ౎ൺ๭ݸ ᆴখ func isSubTree(_ t: TreeNode?, smallerThan val:Int) ->
 Bool


    { guard let t = t else { return true } return t.val < val && isSubTree(t.left, smallerThan: val) && isSubTree(t.right, smallerThan: val) }
  7. ᒾҰҰݸࢠथੋෆੋ౎ൺ๭ݸ ᆴେ func isSubTree(_ t: TreeNode?, greaterThan val:Int) -> 


    Bool
 { guard let t = t else { return true } return t.val > val && isSubTree(t.left, greaterThan: val) && isSubTree(t.right, greaterThan: val) }
  8. func isValidBST(_ root: TreeNode?) -> Bool { guard let root=root

    else { return true } return isSubTree(root.left, smallerThan: root.val) && isSubTree(root.right, greaterThan: root.val) && isValidBST(root.left) && isValidBST(root.right) }
  9. class Solution { func isSubTree(_ t: TreeNode?, smallerThan val:Int) ->

    Bool{ guard let t = t else { return true } return t.val < val && isSubTree(t.left, smallerThan: val) && isSubTree(t.right, smallerThan: val) } func isSubTree(_ t: TreeNode?, greaterThan val:Int) -> Bool{ guard let t = t else { return true } return t.val > val && isSubTree(t.left, greaterThan: val) && isSubTree(t.right, greaterThan: val) } func isValidBST(_ root: TreeNode?) -> Bool { guard let root=root else { return true } return isSubTree(root.left, smallerThan: root.val) && isSubTree(root.right, greaterThan: root.val) && isValidBST(root.left) && isValidBST(root.right) } } Time Complexity: O(n^2)
 Space Complexity: O(1)
 Run Time: 122 ms
  10. Ճ଎ • ׵Ұݸ૝๏ɼ㑌ݸઅᴍ
 తᆴ౎။հԙ๭ݸൣᅴ
 ೭ؒɻ • 8 తࠨᬑཁൺ8 খɼ
 8

    తӈᬑཁൺ8 େ • 3 తࠨᬑཁൺ3 খɼ
 3 తӈᬑཁൺ3 େ • ॴҎ 6 Ṝݸઅᴍཁհԙ 3 ౸ 8 ೭ؒ
  11. (- ∞ ,+∞) (- ∞ ,8) (- ∞ ,3) (3

    ,8) (6 ,8) (3 ,6) ( 8,+∞) ( 10,+∞) ( 10, 14)
  12. class Solution { func checkBound(_ root: TreeNode?, upper: Int, lower:

    Int) -> Bool { guard let root = root else { return true } return root.val < upper && root.val > lower && checkBound(root.left, upper: root.val, lower: lower) && checkBound(root.right, upper: upper, lower: root.val) } func isValidBST(_ root: TreeNode?) -> Bool { guard let root = root else { return true } return checkBound(root, upper: Int.max, lower: Int.min) } } Time Complexity: O(n)
 Space Complexity: O(1)
 Run Time: 65ms
  13. class Solution { func isValidBST(_ root: TreeNode?) -> Bool {

    guard let root = root else { return true } return checkBound(root, upper: Int.max, lower: Int.min) } } extension Solution { func checkBound(_ root: TreeNode?, upper: Int, lower: Int) -> Bool { guard let root = root else { return true } return root.val < upper && root.val > lower && checkBound(root.left, upper: root.val, lower: lower) && checkBound(root.right, upper: upper, lower: root.val) } } Time Complexity: O(n)
 Space Complexity: O(1)
 Run Time: 52 ms
  14. Q&A