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?

分析:求二叉树的中序遍历,採用递归的方法的话很easy,假设非递归的话。就须要用栈来保存上层结点,開始向左走一直走到最左叶子结点,然后将此值输出,从队列中弹出。假设右子树不为空则压入该弹出结点的右孩子,再反复上面往左走的步骤直到栈为空就可以。

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;
}
};

2、Restore IP Addresses

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)

分析:此题跟我之前遇到的一个推断字符串是否是ip地址有点类似,http://blog.csdn.net/kuaile123/article/details/21600189。採用动态规划的方法,參数num表示字符串表示为第几段。假设num==4则表示最后一段。直接推断字符串是否有效,并保存结果就可以,假设不是则点依次加在第0个、第1个....后面,继续递归推断后面的串。

例如以下:

class Solution {
public:
vector<string> restoreIpAddresses(string s) {
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 = num/10){
++size;
}
if(size == len){
return true;
}else{
return false;
}
}
}
};

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses的更多相关文章

  1. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  2. 49. leetcode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  3. 【LeetCode】Binary Tree Inorder Traversal

    Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...

  4. Leetcode 94. Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  5. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  6. Java [Leetcode 94]Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

  7. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...

  8. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  9. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

随机推荐

  1. HDU——T 2647 Reward

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  2. vmware下ubuntu的网络配置

    捣弄了vmnet0和vmnet8连接后,ubuntu上不了网了,今天突然解决了这个问题. 打开vmware后,点编辑-->虚拟网络编辑器 这里我使用的是NAT模式,所以要修改vmnet8 点vm ...

  3. pandas 下的 one hot encoder 及 pd.get_dummies() 与 sklearn.preprocessing 下的 OneHotEncoder 的区别

    sklearn.preprocessing 下除了提供 OneHotEncoder 还提供 LabelEncoder(简单地将 categorical labels 转换为不同的数字): 1. 简单区 ...

  4. 25.Detours劫持技术

    Detours可以用来实现劫持,他是微软亚洲研究院开发出来的工具,要实现它首先需要安装Detours. 安装地址链接:https://pan.baidu.com/s/1eTolVZs 密码:uy8x ...

  5. 【转载】jQuery弹出层始终垂直居中于当前屏幕

    一般网站上肯定有一些弹出框,不论弹出框的大小,都需要他在当前窗口垂直居中.之前手上就有一个jQuery的例子,后来才发现,他只能在第一屏垂直居中,如果滑动滚动条,弹出的框就在上方,不是很方便.请教朋友 ...

  6. 分享一个js对象开发组件写法

    var TextCount = (function(){ //私有方法,外面将访问不到 var _bind = function(that){ that.input.on('keyup',functi ...

  7. 【Linux下安装使用virtualenv与virtualenvwrapper】

    virtualenv virtualenv是一个可以帮助我们管理不同Python环境的绝好工具. virtualenv是一个可以在系统中建立多个不同且相互不干扰的虚拟环境. 下面的步骤将创建两个独立的 ...

  8. HRBUST 1819 石子合并问题--圆形版

    石子合并问题--圆形版 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HRBUST. Original ...

  9. 利用命令行删除Android系统自带应用的方法

    声明:本博客为原创博客.未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(链接为http://blog.csdn.net/bettarwang/article/detai ...

  10. android反编译odex文件

    关于android的反编译工具,相信大家并不陌生 如APK-TOOL,dex2jar APK-TOOL 用于反编译出布局文件 下载地址http://code.google.com/p/android- ...