题目:

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

For example:
Given binary tree{1,#,2,3},

   1\2/3

return[1,3,2].

题目的意思是进行树的中序遍历。很明显在看到关于二叉树的相关题目之后,我们应该首先想到的是递归,如果递归不行就想办法使用队列或者栈。本题使用递归的方法:

 1 import java.util.*;
 2 public class Solution {
 3
 4     public void inorder(ArrayList<Integer> list,TreeNode root)
 5     {
 6         if(root == null)//递归结束条件
 7         {
 8             return;
 9         }
10         inorder(list,root.left);//递归遍历左子树
11         list.add(root.val);//将根节点的值放入list中
12         inorder(list,root.right);//递归遍历右子树
13     }
14     public ArrayList<Integer> inorderTraversal(TreeNode root) {
15         ArrayList<Integer> list = new ArrayList<Integer>();
16         if(root == null)
17         {
18             return list;
19         }
20         inorder(list,root);
21         return list;
22     }
23 }

这道题目相对比较简单,关于二叉树的遍历操作,后期我在数据结构与算法这一栏目会进行相关知识的填充。

转载于:https://www.cnblogs.com/cmh-hw/p/8029018.html

LeetCode---binary-tree-inorder-traversal相关推荐

  1. LeetCode Binary Tree Inorder Traversal

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

  2. [leetcode]Binary Tree Inorder Traversal

    二叉树的中序遍历非递归版本,采用的是wiki百科上的办法.果然简洁,而且和先序有异曲同工之妙,先序只用push右节点,中序只用push中节点.除此之外还有个更改TreeNode加一个visited的b ...

  3. Leetcode: Binary Tree Inorder Traversal

    中序遍历 代码: #include <iostream> #include <vector> using namespace std;struct TreeNode {int ...

  4. 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal

    LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...

  5. LeetCode 94. Binary Tree Inorder Traversal

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

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

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

  7. 15 二叉树的中序遍历(Binary Tree Inorder Traversal)

    文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...

  8. [LeetCode]:94:Binary Tree Inorder Traversal

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

  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. [swift] LeetCode 94. Binary Tree Inorder Traversal

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

最新文章

  1. LeetCode Find All Anagrams in a String
  2. T400的5100无线网卡在Centos下跑起来了
  3. Linux|UNIX下LAMP环境的搭建及常见问题[连载4]
  4. python stdout stderr 一起输出_Python在保留顺序的同时分别从子进程stdout和stderr读取...
  5. 计算机基础知识的最小集合
  6. windbg学习.formats--转换成各种进制
  7. ant 实现批量打包android应用
  8. markdown常见问题
  9. 配置SQL Server AlwaysOn高可用性组
  10. Power BI for Office 365 概览
  11. 如何对西数硬盘固件进行逆向分析(下)
  12. SPOJ 2939 Query on a tree V
  13. 学习3dmax游戏建模一定要美术基础很高吗?资深建模师一语道出重点
  14. 如何撰写总体设计与详细设计文档
  15. 计算机视觉中的数学方法——7. 2 酉空间与酉矩阵
  16. 简易cad导出pdf程序源码
  17. Win10红警如何关闭3d加速?
  18. AT指令对wavecom串口GSM工业手机发送短信(英文和PDU短信)
  19. 认识一下身边的互联网---经典互联网书籍阅读总结
  20. 便携式洁面仪商城质检报告检验标准是什么

热门文章

  1. mysql 字段加减_Mysql常见问题及优化
  2. python3.6和2.7的区别_Python2.7与3.6的一些区别
  3. 保存多序列tiff文件_解码TIFF文件
  4. RabbitMQ添加新用户并支持远程访问
  5. (三)Redis两种持久化方案
  6. Fedora ssh服务,防火墙服务设置
  7. 在CentOS7上使用FastDFS搭建文件服务器
  8. django 更改默认数据库为MySQL
  9. Protobuf动态解析那些事儿
  10. OC之集合的创建及应用