Goat Latin

Title: Goat Latin Source: leetcode.com A sentence S is given, composed of words separated by spaces. Each word consists of lowercase and uppercase letters only. We would like to convert the sentence to “Goat Latin” (a made-up language similar to Pig Latin.) The rules of Goat Latin are as follows: If a word begins with ...

Buddy Strings

Title: Buddy Strings Source: leetcode.com Given two strings A and B of lowercase letters, return true if and only if we can swap two letters in A so that the result equals B. Example 1: Input: A = “ab”, B = “ba” Output: true Example 2: Input: A = “ab”, B = “ab” Output: false ...

Water and Jug Problem

Title: Water and Jug Problem Source: leetcode.com You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two jugs. If z liters of water is measurable, you must have z ...

Delete Node in a Linked List

Title: Delete Node in a Linked List Source: leetcode.com Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. Suppose the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list ...

Verify Preorder Serialization of a Binary Tree 4

Title: Verify Preorder Serialization of a Binary Tree Source: leetcode.com 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 #. _9_ / \ 3 2 / \ ...

Invert Binary Tree

Title: Invert Binary Tree Source: leetcode.com Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 12345      4   /   \  2     7 / \   / \1   3 6   9 to 4 / \ 7 2 / \ / \ 9 6 3 1 12345      4   /   \  7     ...

Maximum Depth of Binary Tree

Title: Maximum Depth of Binary Tree Source: leetcode.com Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Python solution Python ''' https://leetcode.com/problems/maximum-depth-of-binary-tree/ ''' # Definition for a binary tree node. # class TreeNode(object): # ...

Sum Root to Leaf Numbers 1

Title: Sum Root to Leaf Numbers Source: leetcode.com Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. For example, 1 / \ 2 3 123     1   / \  2   ...

Simplify Path

Title: Simplify Path Source: leetcode.com Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where path = "/../"? In this case, you should return "/". Another corner case is the ...

Reverse Linked List II

Title: Reverse Linked List II Source: leetcode.com Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤ length of list. Python solution Python ...