Skip to content

Instantly share code, notes, and snippets.

@walkingtospace
walkingtospace / gist:b376065b390389d06ed9
Created June 18, 2014 16:34
valid binary search tree (inorder traverse)
https://oj.leetcode.com/problems/validate-binary-search-tree/
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
@walkingtospace
walkingtospace / gist:e7e3ab82aaf34587cc78
Created June 22, 2014 04:59
Daily coding practice set #1
void quicksort(int input[], int left, int right) {
if(left < right) {
int pivot = input[right];
int leftIt = left;
int rightIt = right;
while(leftIt < rightIt) {
while(input[leftIt] < pivot) leftIt++;
while(input[rightIt] > pivot) rightIt--;
@walkingtospace
walkingtospace / gist:af00527455583efa5618
Created June 23, 2014 13:54
conrvert sorted array to balanced BST
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*
/*
@walkingtospace
walkingtospace / gist:41a1622bbf0ca21bae7b
Last active August 29, 2015 14:02
evaluate-reverse-polish-notation
https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
/*
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["3", "3", "*"] -> ["9"]
@walkingtospace
walkingtospace / gist:04e4dfc9b272fea1d795
Last active August 29, 2015 14:02
binary search 를 응용한 주어진 값의 인덱스 찾기
int findNeedle(int input[], int start, int end, int needle) {
if(start > end) return -1;
int mid = floor((start+end)/2);
int result;
if(input[mid] == needle) return mid;
else if(input[mid] < needle) {
result = findNeedle(input, mid+1, end, needle);
if(result == -1) {
//Given a list, rotate the list to the right by k places, where k is non-negative.
Node* rotateNode(Node* head, int k) {
if( k<= 0) return head;
Node* first = kth = head;
Node* end = head;
for(int i=0; i<k+1; i++) kth = kth->next;
@walkingtospace
walkingtospace / gist:80cd04ddd6ee9275c27f
Last active August 29, 2015 14:03
populating-next-right-pointers-in-each-node
/*
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/
차분히 생각해보면(대부분의 BST 문제는 recursive로 푼다), left->right는 right!=NULL 체크로 쉽게 연결할 수 있는데
right->parent's sibling's left 연결이 헷갈릴 수 있다.
key는 현재 자신의 sibling이 존재하는지 node는 알수없기 때문에 parent에서 children의 sibling이 존재하는지를 체크해줘서 연결해줘야
한다는 것을 눈치 채는 것.
첫번째시도 : 문제의 조건인 You may only use constant extra space 을 깜빡하고 recursive로 작성
@walkingtospace
walkingtospace / gist:ab5fb96ee8da4a5f9919
Last active August 29, 2015 14:03
convert-sorted-list-to-binary-search-tree
/* Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
[1,2,3,4,5]
3
/ \
2 5
/ /
1 4
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
@walkingtospace
walkingtospace / gist:a83b68304d90ef0c3a3a
Created July 3, 2014 07:05
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) {}
* };
*/