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?
分析:求二叉树的中序遍历,採用递归的方法的话很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 & Restore IP Addresses的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- 【LeetCode】Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Total Accepted: 16406 Total Submissions: 47212My Submissions Given a b ...
- Leetcode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- leetcode 94 Binary Tree Inorder Traversal ----- java
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Java [Leetcode 94]Binary Tree Inorder Traversal
题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- Unity 调用 Android Native 方法(一) 获得Android系统音量
学习雷锋,好榜样,接下来的这一系类教程里,将通过unity来实现Android端的一些常用功能, 不需要在 Asset/Plugins/Android 目录下引用jar包或者aar包,这是重点. us ...
- java 究竟老年代和年轻代的比例为多大合适呢?
眼下我还没有这方面过多的经验,和切身体会 只是以我眼下的水平看来,年轻代不宜大,假设年轻代大会导致转为老年代的时候,老年代撑不下.导致full gc.回收停顿时间过长
- HDU 3555 Bomb(数位DP模板啊两种形式)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 Problem Description The counter-terrorists found ...
- WordPress改动新用户注冊邮件内容--自己定义插件
有些开放用户注冊功能的WordPress站点,可能有这么一项需求,就是用户注冊成功后,系统会分别给站点管理员和新用户发送一封通知邮件.给管理员发送的是新用户的username和Email,给刚刚注冊的 ...
- C语言keywordstatic的绝妙用途
为什么要说static妙,它确实是妙,在软件开发或者单片机开发过程中,大家总以为static就是一个静态变量.在变量类型的前面加上就自己主动清0了.还有就是加上statickeyword的,无论是变量 ...
- javascript对象如何使用
javascript对象如何使用 一.总结 一句话总结:JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... 因为函数是对象,所以自定义对象的创建中有种方法就是函数 1.js中的 ...
- 26. Intellij IDEA 启动项目ClassNotFoundException
转自:https://blog.csdn.net/zhw0596/article/details/81388147 使用Intellij IDEA 的过程中,新创建的项目启动时报 严重: Error ...
- 关于WMware Workstation出现 “”该虚拟机似乎正在使用中“”解决办法
如图 直接到配置文件D:\centos1\这个目录下 将vmdk.lck文化删除 然后重新打开虚拟机便可进入 问题解决
- Facebook下载总结
Facebook是美国的一个社交网络服务网站,至今注册用户已超越20亿,月活用户更是惊人的突破3亿. 这样庞大的一个社交类网站,每日产生的社交数据当然也是非常可观,而这些社交数据,更接近口语,所以是比 ...
- linux sed命令详解 --大量举例
1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...