LeedCode OJ --- Binary Tree Inorder Traversal
今天只是写了递归的版本,因为还没想好怎么用迭代来实现,可以写的过程中,有一点是有疑问的,虽然我的代码可以AC。
问题是:主调函数是可以使用子函数中返回的在子函数中定义的vector. 我认为在被调函数执行结束之后,其分配的空间是应该被释放的,所以在被调函数中定义的变量也是不可以被主调函数中使用的...可能是C++的基础知识又给忘了,有空要拾起来了。
附上代码(递归版):
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> ans;
if (root == NULL) return ans;
if (root->left == NULL && root->right == NULL) {
ans.push_back(root->val);
return ans;
}
if (root->left != NULL) {
ans = inorderTraversal(root->left);
}
ans.push_back(root->val);
if (root->right != NULL) {
vector<int> tmp = inorderTraversal(root->right);
for (unsigned int i = ; i < tmp.size(); i++)
ans.push_back(tmp[i]);
} return ans;
}
};
今天补上用迭代的方法实现了二叉树的中序遍历, 思路就是首先迭代到左子树最底层的节点(二叉树最左边的节点), 并将路径上的节点压入栈中, 然后依次从栈中弹出元素并加入到结果(ans)中, 然后遍历它的右子树。
附上代码(非递归版):
class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
int tail = ;
vector<TreeNode*> Stack;
vector<int> ans;
TreeNode *T = root;
while (T != NULL || tail) {
while (T != NULL) {
tail++;
Stack.push_back(T);
T = T->left;
}
T = Stack[--tail];
Stack.pop_back();
ans.push_back(T->val);
T = T->right;
} return ans;
}
};
LeedCode OJ --- Binary Tree Inorder Traversal的更多相关文章
- LeetCode OJ——Binary Tree Inorder Traversal
http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 树的中序遍历,递归方法,和非递归方法. /** * Definition ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 3月3日(4) Binary Tree Inorder Traversal
原题: Binary Tree Inorder Traversal 和 3月3日(2) Binary Tree Preorder Traversal 类似,只不过变成中序遍历,把前序遍历的代码拿出来, ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal
既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
随机推荐
- java连接达梦数据库的简单代码
1. 引用DmJdbcDriver.jar包 2. 编写代码: String driver= "dm.jdbc.driver.DmDriver"; String url= &quo ...
- compareTo)--list 根据某字段排序
Collections.sort(actList, new Comparator<Act>() { @Override public int compare(Act o1, Act o2) ...
- IO流12 --- 转换流InputStreamReader --- 技术搬运工(尚硅谷)
InputStreamReader 将字节输入流转换为字符输入流 @Test public void test1(){ InputStreamReader isr = null; try { //字节 ...
- Object上的静态方法
内置提供了一个对象为 Object ,也被称之为是构造函数,用来创建对象用的.在 javascript 函数也是对象,是一种可被执行的对象,所以称Object为对象也是可以的.挂在函数上的方法,称之为 ...
- 前端算法题:找出数组中第k大的数字出现多少次
题目:给定一个一维数组,如[1,2,4,4,3,5],找出数组中第k大的数字出现多少次. 例如:第2大的数是4,出现2次,最后输出 4,2 function getNum(arr, k){ // 数组 ...
- dl, dt, dd /line-height /loat /vertical-align 问题
刚刚在看张鑫旭大神的个人网站,看到一篇关于“css瓶颈”的深度好文,地址为:http://www.zhangxinxu.com/wordpress/?p=2523 关于张大神在文章里面提到的四个问题: ...
- Asp.Net Core2.0在linux下发布
一.在linux上新建mvc项目发布 可以参考:https://segmentfault.com/a/1190000012428781 也可以看微软官方文档. 大致步骤如下: 1.在linux下安装. ...
- 基于LSTM对西储大学轴承故障进行分析
这篇文章是小萌新对西储大学轴承故障进行分析,固定特征为故障直径为0.007,电机转速为1797,12k驱动端故障数据(Drive_End)即DE-time.故障类型y值:滚动体故障,内圈故障,3时,6 ...
- Vbulletin Used to Show Malicious Advertisements
In the past, we have seen a massive amount of vBulletin websites compromised through theVBSeo Vulner ...
- Visual studio加载项目时报错 尚未配置为Web项目XXXX指定的本地IIS,需要配置虚拟目录。解决办法。
在SVN上下载工程项目.使用visual studio打开时,出现如下提示: 查找相关资料,解决办法如下: 使用记事本打开工程目录下的.csproj文件.把<UseIIS>False< ...