61. Binary Tree Inorder Traversal
- Binary Tree Inorder Traversal My Submissions QuestionEditorial Solution
Total Accepted: 123484 Total Submissions: 310732 Difficulty: Medium
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?
思路:1.递归
2.迭代
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
if(root==NULL)return v;
vector<int> vl,vr;
vl = inorderTraversal(root->left);
v.push_back(root->val);
vr = inorderTraversal(root->right);
int n = vl.size()+v.size()+vr.size();
vector<int> res(n);
copy(vl.begin(),vl.end(),res.begin());
copy(v.begin(),v.end(),res.begin()+vl.size());
copy(vr.begin(),vr.end(),res.begin()+vl.size()+v.size());
return res;
}
};
以下是迭代方法,多练习以写出一个精简的迭代代码
思路找到左线压入栈,自底向上访问,有右子树的节点跳至右子树重复
上述过程。
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> v;
stack<TreeNode*> st;
while(root||!st.empty()){//第一个处理root情况
while(root!=NULL){ //一直找到最左下角,没有的话后面弹出该元素值
st.push(root);
root = root->left;
}
root = st.top();
st.pop();
v.push_back(root->val);//弹出值,转至右子树
root = root->right;
}
return v;
}
};
61. Binary Tree Inorder Traversal的更多相关文章
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 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 ...
- 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: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- [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 ...
随机推荐
- Spring Cloud Gateway + Jwt + Oauth2 实现网关的鉴权操作
Spring Cloud Gateway + Jwt + Oauth2 实现网关的鉴权操作 一.背景 二.需求 三.前置条件 四.项目结构 五.网关层代码的编写 1.引入jar包 2.自定义授权管理器 ...
- CSP-S 2021 遗言
感谢€€£,谢谢宁嘞! 第一题,€€£给了很多限制条件,什么"先到先得"."只有一个跑道",让它看起来很好做,然后来骗,来偷袭,广大"消费者" ...
- AOP源码解析:AspectJExpressionPointcutAdvisor类
先看看 AspectJExpressionPointcutAdvisor 的类图 再了解一下切点(Pointcut)表达式,它指定触发advice的方法,可以精确到返回参数,参数类型,方法名 1 pa ...
- 有向路径检查 牛客网 程序员面试金典 C++ Python
有向路径检查 牛客网 程序员面试金典 C++ Python 题目描述 对于一个有向图,请实现一个算法,找出两点之间是否存在一条路径. 给定图中的两个结点的指针DirectedGraphNode* a, ...
- heihei
adb shell screencap -p /sdcard/p1.pngadb pull /sdcard/p1.png c:\BaiduYunDownloadadb shell rm /sdcard ...
- SpringCloud微服务实战——搭建企业级开发框架(十二):OpenFeign+Ribbon实现负载均衡
Ribbon是Netflix下的负载均衡项目,它主要实现中间层应用程序的负载均衡.为Ribbon配置服务提供者地址列表后,Ribbon就会基于某种负载均衡算法,自动帮助服务调用者去请求.Ribbo ...
- 【java + selenium3】窗口基本操作及8大定位元素方法总结(一)
一.窗口基本操作 1. 关于窗口的设置都是由window对象提供的: 获取window的对象方法: driver.manage().window(); //1.获取 window 对象 Window ...
- 一文了解cookie
@ 目录 什么是Cookie? Cookie 的作用 Cookie原理 Cookie的分类 会话 Cookies 永久性 Cookies Cookie 的属性 name value Domain Pa ...
- feign微服务调用携带浏览器信息(header、cookie)
import feign.RequestInterceptor; import feign.RequestTemplate; import org.apache.commons.collections ...
- Java踩坑之List的removeAll方法
最近在公司写东西,发现List的removeAll方法报错 Demo代码如下: List<Long> ids1 = Arrays.asList(1L, 3L, 2L); List<L ...