LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [,null,,] \ / Output: [,,]
Follow up: Recursive solution is trivial, could you do it iteratively?
题目中要求使用迭代用法,利用栈的“先进后出”特性来实现中序遍历。
解法一:(迭代)将根节点压入栈,当其左子树存在时,一直将其左子树压入栈,直至左子树为空,将栈顶元素弹出,将其val值放入vector中,再将其右子树循环上述步骤,直到栈为空。
(C++)
vector<int> inorderTraversal(TreeNode* root) {
vector<int> m={};
stack<TreeNode*> stack;
if(!root)
return m;
TreeNode* cur=root;
while(!stack.empty()||cur){
while(cur){
stack.push(cur);
cur=cur->left;
}
cur=stack.top();
m.push_back(cur->val);
stack.pop();
cur=cur->right;
}
return m;
}
方法二:使用递归(C++)
void inorder(vector<int> &m,TreeNode* root){
if(root==NULL)
return;
inorder(m,root->left);
m.push_back(root->val);
inorder(m,root->right);
} vector<int> inorderTraversal(TreeNode* root) {
vector<int> m={};
if(root==NULL)
return m;
inorder(m,root);
return m;
}
LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
- 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- Ubuntu使用总结二
Ubuntu使用 - 1.ubuntu怎么切换到root用户,切换到root账号方法 ubuntu怎么切换到root用户,我们都知道使用su root命令,去切换到root权限,此时会提示输入密码, ...
- idea打开dashboard
1.编辑workspace.xml文件,搜索 “RunDashboard” 节点 2.在component节点下增加option <option name="configuration ...
- vim 常用 NERDTree 快捷键
ctrl + w + h 光标 focus 左侧树形目录 ctrl + w + l 光标 focus 右侧文件显示窗口 ctrl + w + w 光标自动在左右侧窗口切换 ctrl + w + r 移 ...
- web方向编程语言最全对比
web方向编程语言最全对比 目前一般公司的后台用的开发语言大概有以下几种:java,python,php,asp.net,c++,node.js,ruby on rails 等. java 优点:性能 ...
- 提取excel表数据成json格式的以及对图片重命名
开发那边的需求 1.功夫熊猫以及阿狸布塔故事集都是属于剧集的.意思就是有很多集,这里称他们为tv最下面这几行第一列没名字的都是单集的,这里称它们为mv需要统计所有工作表里面的数据把tv放一个大的jso ...
- Kuberneteser二进制安装与配置(二)
环境:Centos7 版本:Kubernetes v1.11.4 一.下载Kubernetes 1)下载 wget https://github.com/kubernetes/kubernet ...
- java中super关键字的作用
1.super关键字可以在子类的构造方法中显示地调用父类的构造方法,super()必须为子类构造函数中的第一行. 2.super可以用来访问父类的成员方法或变量,当子类成员变量或方法与父类有相同的名字 ...
- SourceInsight宏插件2(非常好用,强力推荐)
Quicker宏在SI中的使用方法(下载地址:链接:https://pan.baidu.com/s/1VrDxlPhft7RPUCCOKxsGIg 提取码:2d4u) Quicker宏的添加到SI中 ...
- mac mongodb安装
1.前往官网下载.tgz文件 2.解压 tar zxf mongo压缩文件 3.配置环境变量:MAVEN_HOME & bin路径 4.创建data & log文件夹 5.执行安装命令 ...
- theano安装问题
WARNING (theano.configdefaults): g++ not available, if using conda: `conda install m2w64-toolchain` ...