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] ...
随机推荐
- jdk的下载
1.打开oracle的官网https://www.oracle.com/index.html 2.拖动页面到最后,找到java for developer 并点击 3. 4.拖动到最后找到java A ...
- adv生成控制器手腕位置倾斜原因以及解决方案
系统默认问题导致手腕倾斜详情描述: 手腕部分默认生成轴向是冲向模板下一层级第一个物体 简单说就是 FK轴向冲向模板中指方向 如图 默认模板没问题是因为 默认模板没有改动情况下系统中指与手腕在一条直 ...
- 2018-2019-2 20165313 《网络对抗技术》 Exp5:MSF基础应用
实践使用漏洞和辅助模块 任务一:MS17-010安全漏洞 任务二:(1)MS11-050(失败)(2)MS14-064(唯一) 任务三:abode_flash_avm2 辅助模块:(1)ARP扫描模块 ...
- 2017第八届蓝桥杯C/C++语言A组
一:题目: 标题:迷宫 X星球的一处迷宫游乐场建在某个小山坡上.它是由10x10相互连通的小房间组成的. 房间的地板上写着一个很大的字母.我们假设玩家是面朝上坡的方向站立,则:L表示走到左边的房间,R ...
- linux下的c程序排版工具:indent
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u010839382/article/details/30779523 Linux下有一个方便的c语言 ...
- C 运算符, 有符号数据运算,
1. b++运算 ; ; a = b++ + b++;printf("a=%d\n",a);printf("b=%d\n",b); 输出结果: a=3 b=3 ...
- myql 格式化日期
date_format(a.balance_date,'%Y-%m')= date_format(#{balanceDate},'%Y-%m')
- Centos 7 修改日期和时间的命令
timedatectl set-ntp no //关闭时间动态更新timedatectl set-time "YYYY-MM-DD HH:MM:SS" //设置时间和日期timed ...
- Opencv 图像读取与保存问题
转自 @yhl_leo 1 图像读取 首先看一下,imread函数的声明: // C++: Mat based Mat imread( ); // C: IplImage based IplImage ...
- vue 中 相同的路由不会跳转,更改路由的办法
vue 开发的项目,路由跳转的时候,是相同的路由是不会跳转,页面也不会有更新的 有时候 必须要跳转怎么办, 更改一个参数,num,一直在改变.就可以进入了. this.$router.push({ p ...