Leetcode 練習:2021-12-24

December 24, 2021

Minimum Depth of Binary Tree

題號:111

var minDepth = function(root) {
  if (!root) return 0
  if (!root.left && !root.right) return 1 // 拿掉也可以,但是速度會慢上不少。
  if (!root.left) return 1 + minDepth(root.right)
  if (!root.right) return 1 + minDepth(root.left)
  return Math.min(minDepth(root.right), minDepth(root.left)) + 1
}

Path Sum

題號:112

var hasPathSum = function(root, targetSum) {
  if (!root) return false
  
  const arr = []
  const recur = (node, sum) => {
    sum += node.val
    if (!node.left && !node.right) {
      arr.push(sum)
      return
    }
    if (node.left) recur(node.left, sum)
    if (node.right) recur(node.right, sum)
  }
  recur(root, 0)

  return arr.some(e=>e===targetSum)
}

Merge Intervals

題號:56

var merge = function(intervals) {
  if (intervals.length === 1) return intervals
  const sorted = intervals.sort((a,b)=>a[0]-b[0])
  const result = []
  let interval = sorted[0]
  
  for (var i = 1; i <= sorted.length; i++) {
    if (!sorted[i]) {
      result.push(interval)
      break
    }
    if (sorted[i][0] > interval[1]) {
      result.push(interval)
      interval = sorted[i]
    } else {
      interval[1] = Math.max(sorted[i][1], interval[1])
    }
  } 
  return result
}

Profile picture

Written by Wei Hung who is thinking. You should follow them on Twitter