一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

Given a binary tree, return the inorder traversal of its nodes’ values.

For example:

Given binary tree [1,null,2,3],

1

  \

  2

 /

3

return [1,3,2].

(二)解题

题目大意:给定一个二叉树,输出中序遍历

讲二叉树的时候基本上都讲过递归求解中序遍历。即先访问左子树, 再根结点,再右子树,

/**
 * 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> ret;
        inorder(root,ret);
        return ret;
    }
    void inorder(TreeNode* p,vector<int>& ret)
    {
        if(p==NULL) return;
        inorder(p->left,ret);//访问左子树
        ret.push_back(p->val);//将根节点保存
        inorder(p->right,ret);//访问右子树
    }
};

【一天一道LeetCode】#94. Binary Tree Inorder Traversal的更多相关文章

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

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

  2. 49. leetcode 94. Binary Tree Inorder Traversal

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

  3. Leetcode 94. Binary Tree Inorder Traversal

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

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

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

  5. Java [Leetcode 94]Binary Tree Inorder Traversal

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

  6. Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)

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

  7. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

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

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

  9. leetCode 94.Binary Tree Inorder Traversal(二叉树中序遍历) 解题思路和方法

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

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

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

随机推荐

  1. JavaScript正则表达式模式匹配(2)——分组模式匹配

    var pattern=/google{4,8}$/; // {4,8}$表示匹配结尾4-8次 var str='googleeeeeeeee'; // 表示e的4-8次 alert(pattern. ...

  2. Oracle中表字段相关操作举例

    --创建测试表 create or replace table student ( xh ), --学号 xm ), --姓名 sex ), --性别 birthday date, --日期 sal ...

  3. 上篇:python的基本数据类型以及对应的常用方法(数字、字符串、布尔值)

    为了日后便于查询,本文所涉及到的必记的基本字符串方法如下: "分隔符".join(字符串)    #将字符串的每一个元素按照指定分隔符进行拼接.split("字符串&qu ...

  4. 转:rabbitMQ 安装与管理

    安装环境 虚拟机:VMware® Workstation 10.0.1 build Linux系统:CentOS6.5 官方安装:http://www.rabbitmq.com/install-rpm ...

  5. PHP 5 Filesystem 函数

    PHP Filesystem 简介 Filesystem 函数允许您访问和操作文件系统. 安装 Filesystem 函数是 PHP 核心的组成部分.无需安装即可使用这些函数. Runtime 配置 ...

  6. PHP 5 Calendar 函数

    PHP Calendar 简介 日历扩展包含了简化不同日历格式间的转换的函数. 它是基于 Julian Day Count(儒略日计数),是从公元前 4713 年 1 月 1 日开始计算的. 注释:如 ...

  7. Dockerfile的指令

    指令的一般格式为 INSTRUCTION arguments,指令包括 FROM.MAINTAINER.RUN 等. FROM 格式为 FROM <image>或FROM <imag ...

  8. 解决ASP.NET MVC 检测到有潜在危险的 Request.Form 值

    提交使用html编辑器编辑后的数据,由于Request时出现有HTML或JavaScript等字符串时,系统会认为是危险性值.立马报错. "从客户端 ... 中检测到有潜在危险的 Reque ...

  9. linux:如何指定进程运行的CPU

    coolshell最新的文章<性能调优攻略>在"多核CPU调优"章节,提到"我们不能任由操作系统负载均衡,因为我们自己更了解自己的程序,所以,我们可以手动地为 ...

  10. SpriteKit中反转Action需要注意的问题

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我们知道在SpriteKit中同样有Cocos2D中类似的Ac ...