# leetcode -day29 Binary Tree Inorder Traversal & Restore IP Addresses

1、
﻿﻿

Binary Tree Inorder Traversal

Given a binary tree, return the inorder traversal of its nodes‘ values.

For example:
Given binary tree `{1,#,2,3}`,

```   1
2
/
3
```

return `[1,3,2]`.

Note: Recursive solution is trivial, could you do it iteratively?

```class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> result;
if(!root){
return result;
}
TreeNode* tempNode = root;
stack<TreeNode*> nodeStack;
while(tempNode){
nodeStack.push(tempNode);
tempNode = tempNode->left;
}
while(!nodeStack.empty()){
tempNode = nodeStack.top();
nodeStack.pop();
result.push_back(tempNode->val);
if(tempNode->right){
nodeStack.push(tempNode->right);
tempNode = tempNode->right;
while(tempNode->left){
nodeStack.push(tempNode->left);
tempNode = tempNode->left;
}
}
}
return result;
}
};```

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given `"25525511135"`,

return `["255.255.11.135", "255.255.111.35"]`. (Order does not matter)

```class Solution {
public:
vector<string> result;
int len = s.length();
if(len < 4 || len > 12){
return result;
}
dfs(s,1,"",result);
return result;
}
void dfs(string s, int num, string ip, vector<string>& result){
int len = s.length();
if(num == 4 && isValidNumber(s)){
ip += s;
result.push_back(ip);
return;
}else if(num <= 3 && num >= 1){
for(int i=0; i<len-4+num && i<3; ++i){
string sub = s.substr(0,i+1);
if(isValidNumber(sub)){
dfs(s.substr(i+1),num+1,ip+sub+".",result);
}
}
}
}
bool isValidNumber(string s){
int len = s.length();
int num = 0;
for(int i=0; i<len; ++i){
if(s[i] >= '0' && s[i] <= '9'){
num = num*10 +s[i]-'0';
}else{
return false;
}
}
if(num>255){
return false;
}else{
//非零串首位不为0的判断
int size = 1;
while(num```