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++的更多相关文章

  1. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

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

  2. [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆

    二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...

  3. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

  4. [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历

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

  5. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

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

  6. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  7. Leetcode 94 Binary Tree Inorder Traversal 二叉树

    二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...

  8. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

  9. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

随机推荐

  1. python3-datetime.date详解(一)

    datetime是python操作日期和时间的内置模块. python有两种日期.时间对象:“naive”和“aware”.前者由于忽略了实际情况更容易理解,操作.在任何时间空间内,它的值都取决于一个 ...

  2. Web前端学习第二天(cookie 一)

    CookIe安全 cookie一个神奇的机制,无论什么请求中都会带有cookie字段. 可以通过服务器响应头的Set-Cookie字段添加,修改和删除,大多数情况下,客户端通过JavaScript也可 ...

  3. ACM山东工商 Contest - 软件171-2 第1次测验

    #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node *next ...

  4. 学习笔记TF066:TensorFlow移动端应用,iOS、Android系统实践

    TensorFlow对Android.iOS.树莓派都提供移动端支持. 移动端应用原理.移动端.嵌入式设备应用深度学习方式,一模型运行在云端服务器,向服务器发送请求,接收服务器响应:二在本地运行模型, ...

  5. Jrebel 配置

    先下载插件 http://139.199.89.239:1008/88414687-3b91-4286-89ba-2dc813b107ce http://jrebel.autoseasy.cn/xix ...

  6. Ubuntu 16下单机安装配置zookeeper和kafka

    网上其他的没有一个能直接照做完成的,我这个也是看了些帖子,整出来的怕以后忘记 建议连接工具:Bitvise SSH Client 一.安装配置zookeeper 下载zookeeper 3.4.13: ...

  7. day061 cookie和session

    一. cookie 1.cookie 的原理 工作原理是:浏览器访问服务端,带着一个空的cookie,然后由服务器产生内容, 浏览器收到相应后保存在本地:当浏览器再次访问时,浏览器会自动带上Cooki ...

  8. orchestrator HTTP接口forget-cluster误下线集群问题

    orchestrator 提供了"forget-cluster"HTTP接口用于下线集群.该接口可以根据提供的参数,推测可能的集群名cluster name,然后使用cluster ...

  9. express中遇到的一个小问题“403”

    这样子的一个express简单项目文件执行会出现403 Forbidden的错误: var express = require('express'); var app = express(); app ...

  10. vue 将毫秒转为日期

    12345656546 | parseTime('{y}-{m}-{d} {h}:{i}')