Binary Tree Level Order Traversal

Description https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Difficulty: 0.5/5.0 star Related: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Analysis and solution class Solution { public: vector<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > result; traverse(root, 0, result); return result; } void traverse(TreeNode * pt, int level, vector<vector<int> > & result){ if (pt...

Unique Paths i and ii

Description https://oj.leetcode.com/problems/unique-paths/ Difficulty: 2.5/5.0 star Analysis It is fairly easy to resort to BFS to solve this problem. However, for the first problem, as there is no obstacle at all, this problem should have closed-form combinatorial solution. Supposed that our...

Symmetric Tree

Description https://oj.leetcode.com/problems/symmetric-tree/ Difficulty: 1.5/5.0 star Analysis The first solution provided here is a recursive solution. However, as this is not a divide-and-conquer, but just a element-wise checking problem, we can easily solve it iteratively (via BFS), which is the second...

Balanced Binary Tree

Description https://oj.leetcode.com/problems/swap-nodes-in-pairs/ Difficulty: 1.0/5.0 star Solution Only two moving pointers suffice. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution {...

Sort Colors

Description https://oj.leetcode.com/problems/sort-colors/ Difficulty: 2.5/5.0 star Analysis This problem is pretty trivial if it is not restricted to one sweep. When restricted to one sweep, we use three indice to track the “progress” of each color. Actually it is hard to...