Java二叉树前中后序的非递归实现
大家好,我是晓星航。今天为大家带来的是 Java二叉树前中后序的非递归实现 的讲解!😀
♈️1.二叉树前序非递归遍历实现♈️
二叉树前序非递归遍历实现 OJ链接
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ /* class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> list = new ArrayList<>(); if (root == null) { return list; } list.add(root.val); List<Integer> leftTree = preorderTraversal(root.left); list.addAll(leftTree); List<Integer> rightTree = preorderTraversal(root.right); list.addAll(rightTree); return list; } }*/ class Solution { public List<Integer> preorderTraversal(TreeNode root) { List<Integer> ret = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; while (cur != null || !stack.isEmpty()) { while (cur != null) { stack.push(cur); // System.out.print(cur.val + " "); ret.add((int) cur.val); cur = cur.left; } TreeNode top = stack.pop(); cur = top.right; } return ret; } }
思路:创建一个树的结点cur来遍历我们的二叉树,并在cur每遍历一个元素,我们的ret链表就添加一个cur遍历的元素的值并将该元素加入我们的栈中,然后在二叉树左边遍历完毕后我们开始弹出左边的元素并开始遍历右边的元素,在cur(遍历元素)和栈中元素不为空时一直循环即可。(根左右)
♉️2.二叉树中序非递归遍历实现♉️
二叉树中序非递归遍历实现OJ链接
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> ret = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; while(cur != null || !stack.isEmpty()) { while (cur != null) { stack.push(cur); cur = cur.left; } TreeNode top = stack.pop(); ret.add((int)top.val); cur = top.right; } return ret; } }
思路:创建一个树的结点cur来遍历我们的二叉树,并用cur遍历每一个元素,在二叉树左边遍历完毕后,我们的ret链表就添加一个cur遍历的元素的值并将该元素加入我们的栈中,然后我们开始弹出左边的元素并开始遍历右边的元素,在cur(遍历元素)和栈中元素不为空时一直循环即可。(左根右)
♋️3.二叉树后序非递归遍历实现♋️
二叉树后序非递归遍历实现OJ链接
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<Integer> postorderTraversal(TreeNode root) { List<Integer> ret = new ArrayList<>(); Stack<TreeNode> stack = new Stack<>(); TreeNode cur = root; TreeNode prev = null; while (cur != null || !stack.isEmpty()) { while (cur != null) { stack.push(cur); cur = cur.left; } TreeNode top = stack.peek(); if (top.right == null || top.right == prev) { stack.pop(); ret.add(top.val); prev = top; } else { cur = top.right; } } return ret; } }
思路:创建一个树的结点cur来遍历我们的二叉树,并用cur遍历每一个元素,在二叉树左边遍历完毕后,我们开始弹出左边的元素并开始遍历右边的元素,在左右元素都遍历完后我们的ret链表就添加一个cur遍历的元素的值并将该元素加入我们的栈中,在cur(遍历元素)和栈中元素不为空时一直循环即可。(左右根)
感谢各位读者的阅读,本文章有任何错误都可以在评论区发表你们的意见,我会对文章进行改正的。如果本文章对你有帮助请动一动你们敏捷的小手点一点赞,你的每一次鼓励都是作者创作的动力哦!😘