LeetCode---binary-tree-inorder-traversal
题目:
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相关推荐
- LeetCode Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [leetcode]Binary Tree Inorder Traversal
二叉树的中序遍历非递归版本,采用的是wiki百科上的办法.果然简洁,而且和先序有异曲同工之妙,先序只用push右节点,中序只用push中节点.除此之外还有个更改TreeNode加一个visited的b ...
- Leetcode: Binary Tree Inorder Traversal
中序遍历 代码: #include <iostream> #include <vector> using namespace std;struct TreeNode {int ...
- 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal
LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...
- LeetCode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- [LeetCode] Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- 15 二叉树的中序遍历(Binary Tree Inorder Traversal)
文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...
- [LeetCode]:94:Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- leetcode[94]Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [swift] LeetCode 94. Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
最新文章
- LeetCode Find All Anagrams in a String
- T400的5100无线网卡在Centos下跑起来了
- Linux|UNIX下LAMP环境的搭建及常见问题[连载4]
- python stdout stderr 一起输出_Python在保留顺序的同时分别从子进程stdout和stderr读取...
- 计算机基础知识的最小集合
- windbg学习.formats--转换成各种进制
- ant 实现批量打包android应用
- markdown常见问题
- 配置SQL Server AlwaysOn高可用性组
- Power BI for Office 365 概览
- 如何对西数硬盘固件进行逆向分析(下)
- SPOJ 2939 Query on a tree V
- 学习3dmax游戏建模一定要美术基础很高吗?资深建模师一语道出重点
- 如何撰写总体设计与详细设计文档
- 计算机视觉中的数学方法——7. 2 酉空间与酉矩阵
- 简易cad导出pdf程序源码
- Win10红警如何关闭3d加速?
- AT指令对wavecom串口GSM工业手机发送短信(英文和PDU短信)
- 认识一下身边的互联网---经典互联网书籍阅读总结
- 便携式洁面仪商城质检报告检验标准是什么