94 Binary Tree Inorder Traversal
递归的代码是以前数据结构书上常见的:
public ArrayList<Integer> inorderTraversal(ConstructBinaryTreefromPostorderandInorderTraversal.TreeNode root) {ArrayList<Integer> res = new ArrayList<>();dfs(res, root);return res;}private void dfs(List<Integer> res, ConstructBinaryTreefromPostorderandInorderTraversal.TreeNode node) {if (node == null) return;dfs(res, node.left);res.add(node.val);dfs(res, node.right);}
复制代码
非递归用stack模拟中序遍历,要理解啊,不能死记硬背。注意while条件和while里面的if。
public class Solution {public ArrayList<Integer> inorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<>();LinkedList<TreeNode> stack = new LinkedList<>();//注意while条件是或while (root != null || !stack.isEmpty()){if (root!=null){stack.push(root);root = root.left;}else {root = stack.pop();res.add(root.val);root = root.right;}}return res;}
}
复制代码
MAR 25TH 这题今天做98. Validate Binary Search Tree 的时候又来复习了一遍,又忘的差不多了。。记得当时还在考虑为什么while里面要用||而不是&&。
这题还可以这样写:
public List<Integer> inorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if(root == null) return list;Stack<TreeNode> stack = new Stack<>();while(root != null || !stack.empty()){while(root != null){stack.push(root);root = root.left;}root = stack.pop();list.add(root.val);root = root.right;}return list;
}
复制代码
JULY 29 REVIEW
又看了一遍迭代的方法,仍然写不出。。上面那个代码,两个while循环其实挺清晰的,但是root = root.right那边还是挺难想到的。还有就是外层的while,两种情况;1是root!= null的情况,这种比较好考虑,就是首次进入的时候;2是root==null的情况,statck不为空,这种就是dfs到栈最底的情况。
转载于:https://juejin.im/post/5a3132fef265da432d281a4b
94 Binary Tree Inorder Traversal相关推荐
- LeetCode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...
- 【二叉树迭代版中序遍历】LeetCode 94. Binary Tree Inorder Traversal
LeetCode 94. Binary Tree Inorder Traversal Solution1:递归版 二叉树的中序遍历递归版是很简单的,中序遍历的迭代版需要特殊记一下! 迭代版链接:htt ...
- [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 ...
- 94. Binary Tree Inorder Traversal
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...
- 94. Binary Tree Inorder Traversal二叉树的中序遍历
网址:https://leetcode.com/problems/binary-tree-inorder-traversal/ 参考:https://leetcode.com/problems/bin ...
- 94. Binary Tree Inorder Traversal 二叉树的中序遍历
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3]1\2/3输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 二叉树遍历 不太想用递归,试试循环吧 ...
- LeetCode 94. Binary Tree Inorder Traversal--二叉树中序遍历--递归,迭代--C++,Python解法
题目地址:Binary Tree Inorder Traversal - LeetCode Given a binary tree, return the inorder traversal of i ...
最新文章
- PHP 中提示undefined index如何解决(多种方法)
- 7 1学会使用 Node 编写简单的前端应用
- linux 切换用户_Linux 用户态切换到内核态的 3 种方式
- vue下的props,data
- 【pyradiomics学习】——影像组学特征
- js字符串string转object对象 - 方法篇
- 7-13 简单计算器 (25 分)
- SpringMVC系列一
- CDOJ 485 UESTC 485 	Game (八数码变形,映射,逆cantor展开)
- CS224N笔记——Word Window分类与神经网络
- 计算机编程必备英语单词,编程序常用英语单词
- python 人脸识别活体检测_手把手教你用Python实现人脸识别,辨别真假!
- 广电优点家庭服务器怎么无线桥接,路由器有线桥接和无线桥接哪个好一点?
- 关于Python包导入报错的总结
- 筱筱看博客(简易的 loading 封装)
- 以txt为数据源的随机点名系统
- 图片作为背景的相关方法
- e3d教程做logo教程_AE-炫酷LED灯动画 LOGO片头制作(E3D插件)
- 【问题解决】Springboot中@Value()读取不到配置文件属性解决方法
- 香帅的北大金融学课笔记15 -- 大师投资智慧