Minimum Depth of Binary Tree

Description https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Difficulty: 0.5/5.0 Analysis and solution /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution {...

Length of Last Word

Description https://oj.leetcode.com/problems/length-of-last-word/ Difficulty: 1.0/5.0 Analysis and solution We count the length of each word sequentially and keep the last one as the result; class Solution { public: int lengthOfLastWord(const char *s) { int len = 0; int lenS = strlen(s);...

Flatten Binary Tree to Linked List

Description https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ Difficulty: 2.5/5.0 star Analysis and solution The trick here is that we have to cache the right pointer before assigning new values to it. Besides pre-order flatten, we can also do in-order flatten or post-order flatten too. To...

N-Queens i and ii

Analysis and solution The solutions to question i and question ii are almost the same. We iterate through each row, by filling one ‘Q’ to each row without more than on ‘Q’ being on the same column and any diagonal...

N Queens I And Ii

// Solution to N-Queens i class Solution { public: vector<vector<string> > solveNQueens(int n) { vector<vector<string> > result; vector<string> tmp; bool diag[4][n], * diagProxy[4]; bool col[n]; for (int i = 0; i < 4; ++ i){ memset(diag[i], 0, sizeof(bool) * n);...