lintcode:二叉树的中序遍历
题目:
二叉树的中序遍历
给出一棵二叉树,返回其中序遍历
给出二叉树 {1,#,2,3}
,
1\2/3
返回 [1,3,2]
.
你能使用非递归算法来实现么?
解题:
程序直接来源
Java程序:
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left = this.right = null;* }* }*/ public class Solution {/*** @param root: The root of binary tree.* @return: Inorder in ArrayList which contains node values.*/public ArrayList<Integer> inorderTraversal(TreeNode root) {// write your code hereArrayList<TreeNode> p = new ArrayList<TreeNode>(); ArrayList<Integer> res = new ArrayList<Integer>(); while(root != null || p.size() != 0){ while(root != null){ p.add(root); root = root.left; } root = p.get(p.size()-1); p.remove(p.size()-1); res.add(root.val); root = root.right; } return res; }}
View Code
总耗时: 1238 ms
Python程序:
""" Definition of TreeNode: class TreeNode:def __init__(self, val):self.val = valself.left, self.right = None, None """class Solution:"""@param root: The root of binary tree.@return: Inorder in ArrayList which contains node values."""def inorderTraversal(self, root):# write your code herep = [root] res = [0] while root is not None or len(p) != 1: while root is not None: p.append(root) root = root.left root = p[len(p)-1] del p[len(p)-1] res.append(root.val) root = root.right n = len(res) return res[1:n]
View Code
总耗时: 263 ms
非递归程序,理解不透,还需要人丑就要多读书
根据上面灵感,递归程序如下:
java程序:
/*** Definition of TreeNode:* public class TreeNode {* public int val;* public TreeNode left, right;* public TreeNode(int val) {* this.val = val;* this.left = this.right = null;* }* }*/ public class Solution {/*** @param root: The root of binary tree.* @return: Inorder in ArrayList which contains node values.*/public ArrayList<Integer> inorderTraversal(TreeNode root) {// write your code here ArrayList<Integer> res = new ArrayList<Integer>(); res = inorderTrun(res,root);return res; }public ArrayList<Integer> inorderTrun(ArrayList<Integer> res,TreeNode root){if(root == null)return res;if(root!=null){if(root.left!=null){res = inorderTrun(res,root.left);}res.add(root.val);if(root.right!=null){res = inorderTrun(res,root.right);}}return res;}}
View Code
总耗时: 1714 ms
Python程序:
""" Definition of TreeNode: class TreeNode:def __init__(self, val):self.val = valself.left, self.right = None, None """class Solution:"""@param root: The root of binary tree.@return: Inorder in ArrayList which contains node values."""def inorderTraversal(self, root):# write your code hereres = []res = self.inorderTrun(res,root)return resdef inorderTrun(self,res,root):if root==None:return resif root.left!=None:res = self.inorderTrun(res,root.left)res.append(root.val)if root.right!=None:res = self.inorderTrun(res,root.right)return res
View Code
总耗时: 213 ms
根据上面的程序理解,可根据栈实现,上面定义的ArrayList也是起到栈的作用
lintcode:二叉树的中序遍历相关推荐
- 15 二叉树的中序遍历(Binary Tree Inorder Traversal)
文章目录 1 题目 2 描述 3 解决方案 3.1 递归算法 3.1.1 遍历法(Traverse) 思路 源码 3.1.2 分治法(Devide And Conquer) 思路 源码 3.2 非递归 ...
- 《LeetCode力扣练习》第94题 二叉树的中序遍历 Java
<LeetCode力扣练习>第94题 二叉树的中序遍历 Java 一.资源 题目: 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 1: 输入:root = [1,nul ...
- 树结构练习——排序二叉树的中序遍历(二叉搜索树)
树结构练习--排序二叉树的中序遍历 Time Limit: 1000MS Memory limit: 65536K 题目描述 在树结构中,有一种特殊的二叉树叫做排序二叉树,直观的理解就是--(1).每 ...
- LeetCode-二叉树-94. 二叉树的中序遍历
描述 94. 二叉树的中序遍历 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 1: 输入:root = [1,null,2,3] 输出:[1,3,2] 示例 2: 输入:root ...
- 树结构练习——排序二叉树的中序遍历
树结构练习--排序二叉树的中序遍历 Description 在树结构中,有一种特殊的二叉树叫做排序二叉树,直观的理解就是--(1).每个节点中包含有一个关键值 (2).任意一个节点的左子树(如果存在的 ...
- LeetCode 92反转链表Ⅱ93复制ip地址94二叉树的中序遍历
微信搜一搜:bigsai 专注于Java.数据结构与算法,一起进大厂不迷路! 算法文章题解全部收录在github仓库bigsai-algorithm,求star! 关注回复进群即可加入力扣打卡群,欢迎 ...
- 二叉树的中序遍历非递归方法(算法导论第三版12.1-3)
二叉树的中序遍历非递归方法(算法导论第三版12.1-3) 1⃣️用栈实现 template<typename T> void inorder_tree_walk_non_recursion ...
- leetcode - 94. 二叉树的中序遍历
给定一个二叉树,返回它的中序遍历.中序遍历(LDR)是二叉树遍历的一种,也叫做中根遍历.中序周游.在二叉树中,中序遍历首先遍历左子树,然后访问根结点,最后遍历右子树. 示例: 进阶: 递归算法很简单, ...
- LeetCode-94. 二叉树的中序遍历
题目描述: 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 输入:root = [1,null,2,3] 输出:[1,3,2] 思路分析: 用递归或是迭代算法来解决. 递归: 首先我们需要 ...
最新文章
- Linux脚本实战之检测网卡流量
- 关于web技术的一些见解
- 公司用云桌面还是台式计算机好,相比于性能更强的台式机为什么很多企业更喜欢用云桌面...
- 几种常见的集成触发器(D、T、JK)
- 将字符转换成数字(atoi),将数字转换成字符(itoa)
- anaconda进出某个环境
- php如何根据ip查找地址,根据IP地址查找IP所在地
- Sleutel:密码治理器
- pyqtSignal()
- Java NIO框架Netty教程(四) – ServerBootStrap启动流程源码分析
- Spring源码之getBean(获取 bean)方法(一)解读
- python输出名字和字数_Python字数和排名
- oracle 函数 if 判断,oracle 判断中文函数
- 想找个娇小可爱的女孩
- SIFT--特征描述符
- 【论文】联邦学习区块链 论文集(一)
- dell服务器装独立显卡无显示输出,dell服务器设置独立显卡(dell服务器加显卡)...
- 适用于Windows/Mac的最好视频剪辑软件
- 软考高项:信息系统项目管理师试题(2022年11月)
- imx6ul链接地址、运行地址、加载地址、位置无关、mmu的关系