# Daily Archives: October 8, 2015

## [leetcode] Lowest Common Ancestor of a Binary Tree

Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T […]

## [leetcode] Strobogrammatic Number && II && III

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. For example, the numbers “69”, “88”, and “818” are all strobogrammatic.     class Solution […]

## [leetcode] Course Schedule II

There are a total of n courses you have to take, labeled from 0 to n – 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a list of prerequisite pairs, return the […]

## [leetcode] Course Schedule

Course Schedule There are a total of n courses you have to take, labeled from 0 to n – 1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a list of prerequisite pairs, […]

## [leetcode] Wildcard Matching

Wildcard Matching Implement wildcard pattern matching with support for ‘?’ and ‘*’. ‘?’ Matches any single character. ‘*’ Matches any sequence of characters (including the empty sequence). The matching should cover the entire input string (not partial). The function prototype should be: bool isMatch(const char *s, const char *p) Some examples: isMatch(“aa”,”a”) → […]

## [leetcode] Kth Smallest Element in a BST

Kth Smallest Element in a BST Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST’s total elements. Follow up: What if the BST is modified (insert/delete operations) often and you need to […]

## [leetcode] Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, given [0,1,2,4,5,7], return [“0->2″,”4->5″,”7”]. Credits: Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.     class Solution { public: vector<string> summaryRanges(vector<int>& nums) { vector<string> ans; string range; if(nums.size() == 0) […]