Leetcode 94

时间:2022-09-04 09:41:19
/**
* Definition for a binary tree node.
* 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> res;
DFS(root,res);
return res; }
void DFS(TreeNode* root,vector<int>& res){
if(root == NULL)return;
DFS(root->left,res);
res.push_back(root->val);
DFS(root->right,res);
return;
}
};

___stack__

/**
* Definition for a binary tree node.
* 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> res;
stack<TreeNode*> st;
TreeNode* p = root;
while(p || !empty(st)){ //p的右面没有节点了,然后stack中也没有点了,说明结束遍历了
while(p){
st.push(p);
p = p->left;
}
p = st.top();
st.pop();
res.push_back(p->val);
p = p->right;
}
return res;
}
};

_

Leetcode 94的更多相关文章

  1. &lbrack;leetcode&rsqb; 94&period; Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  2. LeetCode 94&period; 二叉树的中序遍历&lpar;Binary Tree Inorder Traversal&rpar;

    94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...

  3. LeetCode 94 &vert; 基础题,如何不用递归中序遍历二叉树?

    今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...

  4. 49&period; leetcode 94&period; Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal    二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack

  5. &lbrack;LeetCode&rsqb;94&comma; 144&comma; 145 Binary Tree InOrder&comma; PreOrder&comma; PostOrder Traversal&lowbar;Medium

    Given a binary tree, return the inorder, preorder, postorder traversal of its nodes' values. Example ...

  6. Java实现 LeetCode 94 二叉树的中序遍历

    94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...

  7. Leetcode 94&period; Binary Tree Inorder Traversal

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  8. Binary Tree Inorder Traversal -- LeetCode 94

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  9. leetcode 94 Binary Tree Inorder Traversal ----- java

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  10. Java &lbrack;Leetcode 94&rsqb;Binary Tree Inorder Traversal

    题目描述: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given bina ...

随机推荐

  1. nginx的初步了解

    今天学习了nginx.nginx不但可以作为服务器,类似于IIS,也可以作为反向代理.它有一个配置文件nginx.conf,在这个文件里配置了一些重要的参数,通过修改这些参数,然后启动nginx,就可 ...

  2. 生成唯一编号&lpar;序列号&rpar;--sql存储过程

    CREATE procedure [dbo].[P_Sys_GetSerialNo] --取业务序列号 @SeqType int, --序列号类别,4位数,如:10+2+1 即1021 , --要取的 ...

  3. SQL&ast;Loader使用详解(一)

    1.SQL*Loader介绍 1)SQL*Loader是一个从外部文件指加载数据到Oracle数据库的工具.语法类似于DB2的Load语法,但SQL*Loader支持各种load格式.选择性load和 ...

  4. HDU-4642 Fliping game 简单博弈

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4642 看右下角的状态就行了,因为上面的操作每次都会改变它,而最后的局势是根据它来的... //STAT ...

  5. web api&bsol; protobuf &bsol;formatter

    9:50:20吴X2014/11/7 9:50:20 9:55:14吴X2014/11/7 9:55:14webapi实现protobuf吴X2014/11/7 9:56:29http://www.s ...

  6. Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状)

    Cocos2d-x-Lua 开发一个简单的游戏(记数字步进白色块状) 本篇博客来给大家介绍怎样使用Lua这门语言来开发一个简单的小游戏-记数字踩白块. 游戏的流程是这种:在界面上生成5个数1~5字并显 ...

  7. C&num; 判断文件的真实格式

    为了防止图片木马,通过后缀判断文件的格式是不准确的.可以通过这种方式进行判断. static void Main(string[] args) { string path = @"C:\Us ...

  8. 为Android系统内置C可执行程序测试Linux内核驱动程序

    在前一篇文章中,我们介绍了如何在Ubuntu上为Android系统编写Linux内核驱动程序.在这个名为hello的Linux内核驱动程序中, 创建三个不同的文件节点来供用户空间访问,分别是传统的设备 ...

  9. win10切换AHCI模式

    win10切换AHCI模式 笔记本电脑总是卡卡的,开机好慢,一狠心就买了一个固态硬盘装上.听说电脑开启AHCI模式跟固态硬盘更配哦.所以好好得鼓捣了一下电脑. 保证win10开启了安全模式, 如果没有 ...

  10. Java 爬虫学习

    Java爬虫领域最强大的框架是JSoup:可直接解析具体的URL地址(即解析对应的HTML),提供了一套强大的API,包括可以通过DOM.CSS选择器,即类似jQuery方式来取出和操作数据.主要功能 ...