LeetCode- 树的前序、中序、后序遍历(递归与迭代)
来源:互联网 发布:广州烘焙培训 知乎 编辑:程序博客网 时间:2024/06/06 16:31
144. Binary Tree Preorder Traversal
前序遍历
Given a binary tree, return the preorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [1,2,3]
.
Note: Recursive solution is trivial, could you do it iteratively?
递归
public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if (root == null)return list;preorder(root, list);return list;}private void preorder(TreeNode root, List<Integer> list) {if (root != null) {list.add(root.val);preorder(root.left, list);preorder(root.right, list);}
迭代
public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if (root == null)return list;Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()) {//出栈TreeNode node=stack.pop();list.add(node.val);//访问右子树if(node.right!=null)stack.push(node.right);//左子树if(node.left!=null)stack.push(node.left);}return list;}
94. Binary Tree Inorder Traversal
中序遍历
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]
.
Note: Recursive solution is trivial, could you do it iteratively?
递归
public List<Integer> inorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if (root == null)return list;inorder(root, list);return list;}private void inorder(TreeNode root, List<Integer> list) {if (root != null) {inorder(root.left, list);list.add(root.val);inorder(root.right, list);}}
迭代
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.isEmpty()) {if (root != null) {stack.push(root);root = root.left;} else {root = stack.pop();list.add(root.val);root = root.right;}}return list;}
145. Binary Tree Postorder Traversal
后序遍历
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3}
,
1 \ 2 / 3
return [3,2,1]
.
Note: Recursive solution is trivial, could you do it iteratively?
递归
public List<Integer> postorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if (root == null)return list;postorder(root, list);return list;}private void postorder(TreeNode root, List<Integer> list) {if (root != null) {postorder(root.left, list);postorder(root.right, list);list.add(root.val);}}
迭代
关键点是找一个flag存储刚刚访问过的结点
public List<Integer> postorderTraversal(TreeNode root) {List<Integer> list = new ArrayList<>();if (root == null)return list;TreeNode p = root;Stack<TreeNode> stack = new Stack<>();// 辅助指针用于标记最近访问的结点,// 标记是从左子树返回还是右子树返回TreeNode flag = null;while (p != null || !stack.isEmpty()) {if (p != null) {// 走到最左边stack.push(p);p = p.left;}// 向右else {// 去栈顶结点p = stack.peek();// 若右子树存在且未被访问过if (p.right != flag && p.right != null) {// 转向右p = p.right;stack.push(p);// 在走到最左p = p.left;} else {// 访问p = stack.pop();list.add(p.val);// 记录最近访问的结点flag = p;// 结点访问完毕后重置指针pp = null;}}}return list;}
0 0
- LeetCode- 树的前序、中序、后序遍历(递归与迭代)
- 树的遍历实现,前序遍历,中序遍历,后序遍历以及层次遍历的迭代与递归实现
- 二叉树的遍历(先序/中序/后序,递归/迭代)与搜索
- 二叉树的前序、中序、后序遍历(递归与非递归)
- 二叉树的递归与非递归遍历(前序、中序、后序)
- 二叉树的递归与非递归遍历(前序、中序、后序)
- 二叉树的递归与非递归遍历(前序、中序、后序)
- 二叉树的递归与非递归遍历(前序、中序、后序)
- 【数据结构】二叉树(前、中、后)序遍历的递归与非递归算法
- JAVA实现二叉树的前、中、后序遍历(递归与非递归)
- 【二叉树】 前序、中序和后序的递归遍历与非递归遍历
- 二叉树的前序,中序,后序的递归与非递归遍历
- 【二叉树遍历算法】——前/中/后序递归与非递归的实现
- Java 二叉树的前序、中序、后续遍历 递归和迭代实现
- 二叉树遍历(前序、中序、后序)递归与非递归
- 二叉树前、中、后序遍历(递归与非递归)
- java语言实现二叉树的前序、中序与后序遍历(递归与非递归) 层次遍历
- 二叉树的前序、中序、后序的递归与非递归遍历算法实现(待完善)
- Java虚拟机基础知识
- volley+swiperefreshlayout实现checkbox全选
- Android 异步消息处理机制 Handler、Looper、Message、MessageQueue之藕断丝连
- 表单验证插件:jQuery Validation Engine
- nyoj 1277 Decimal integer conversion
- LeetCode- 树的前序、中序、后序遍历(递归与迭代)
- 线程池的使用
- Could not open lock file/var/lib/dpkg/lock的解决办法
- 数据库
- python 图形pygame模板的使用
- 数据库在系统命令执行程序的使用
- 面向对象
- 深入解析java虚拟机
- Java编写作业检查器