## 题目1 序列化二叉树

### 代码

void Serialize(BinaryTreeNode* pRoot, ostream& stream){ if(pRoot==NULL) { stream<<"\$"; return; } stream<<pRoot->val<<','; Serialize(pRoot->left,stream); Serialize(pRoot->right,stream);}void Deserialize(BinaryTreeNode* pRoot, istream& stream){ int number; if(ReadStream(stream,&number)) { *pRoot=new BinaryTreeNode(); (*pRoot)->val=number; (*pRoot)->left=NULL; (*pRoot)->right=NULL; Deserialize(&((*pRoot)->left),stream); Deserialize(&((*pRoot)->right),stream); }}

## 题目2 平衡二叉树

### 代码1

class Solution {public: bool IsBalanced_Solution(TreeNode* pRoot) { if(pRoot == NULL) return true; int leftDepth = getDepth(pRoot -> left); int rightDepth = getDepth(pRoot -> right); if(leftDepth > rightDepth + 1 || leftDepth + 1 < rightDepth) return false; else return IsBalanced_Solution(pRoot -> left) && IsBalanced_Solution(pRoot -> right); } int getDepth(TreeNode* pRoot){ if(pRoot == NULL) return 0; int leftDepth = getDepth(pRoot -> left); int rightDepth = getDepth(pRoot -> right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; }};

### 代码2

bool IsBalanced(BinaryTreeNode* pRoot, int *pDepth){ if(pRoot==NULL) { *pDepth=0; return true; } int left,right; if(IsBalanced(pRoot->left,&left && IsBalanced(pRoot->right,&right)) { int diff=left-right; if(diff<=1 &&diff>=-1) { *pDepth=1+(left>right?left:right); return true; } } return false;}

## 题目3 和为S的连续正数序列

### 代码

vector<vector<int> > FindContinuousSequence(int sum) { int l = 1,r = 1,sumx = 1; vector<vector<int> > ans; while(l <= r){ r ++; sumx += r; while(sumx > sum){ sumx -= l; l ++; } if(sumx == sum && l != r){ vector<int> tmp; for(int i = l;i <= r;i ++) tmp.push_back(i); ans.push_back(tmp); } } return ans;}

## 题目4 孩子们的游戏（约瑟夫环问题）

### 题目解析

k+1 -> 0
k+2 -> 1

n-1 -> n-k-2
0 -> n-k-1

k-1
-> n-2

0 n=1
f(n,m)={
[f(n-1,m)+m]%n n>1

### 代码

int LastRemaining_Solution(int n, int m){ if(n==0) return -1; int s = 0; for(int i = 2;i <= n; i++) { s = (s+m) % i; } return s;}