Skip to content

Instantly share code, notes, and snippets.

@xiren-wang
xiren-wang / binary_tree_level_order_traversal.cpp
Last active August 29, 2015 14:05
binary tree level order traversal.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).
@xiren-wang
xiren-wang / populate_next_right_pointer_2.cpp
Created August 29, 2014 07:00
populate next right pointer 2. For any binary tree, with const space.
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
@xiren-wang
xiren-wang / populate_next_right_pointer_in_each_node.cpp
Last active August 29, 2015 14:05
populate next right pointer in each node.
/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
* Given a binary tree
struct TreeLinkNode {
@xiren-wang
xiren-wang / subsets.cpp
Created August 27, 2014 07:14
subset. Given a set of distinct integers, S, return all possible subsets.
/*
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
@xiren-wang
xiren-wang / partition_list.cpp
Created August 27, 2014 06:56
partition list.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
* Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.
You should preserve the original relative order of the nodes in each of the two partitions.
@xiren-wang
xiren-wang / convert_sorted_array_to_BST.cpp
Created August 27, 2014 06:40
convert sorted array to binary search tree
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@xiren-wang
xiren-wang / path_sum.cpp
Created August 21, 2014 05:41
path sum. Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
@xiren-wang
xiren-wang / sum_root_to_leaf_numbers.cpp
Created August 21, 2014 04:54
sum root to leaf numbers.
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
* Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
@xiren-wang
xiren-wang / sqrt_of_int.cpp
Created August 20, 2014 07:30
sqrt of int
class Solution {
public:
int sqrt(int x) {
//binary search
//Note: must pay attention to overflow, like (int) * (int) easily overflow
// to avoid dead loop, use (low + 1 < high), instead of lwo < high
if (x < 0)
return -1;
if (x == 0)
return 0;
@xiren-wang
xiren-wang / remove_duplicates_2.cpp
Created August 20, 2014 06:51
Remove Duplicates from Sorted List II.
/**
* Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,
Given 1->2->3->3->4->4->5, return 1->2->5.
Given 1->1->1->2->3, return 2->3.
*
* Definition for singly-linked list.
* struct ListNode {
* int val;