二叉树的迭代遍历
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
> 听说还可以用非递归的方式 # 二叉树的迭代遍历 ## 算法公开课 [《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html): * **[写出二叉树的非递归遍历很难么?(前序和后序)](https://www.bilibili.com/video/BV15f4y1W7i2)** * **[写出二叉树的非递归遍历很难么?(中序))](https://www.bilibili.com/video/BV1Zf4y1a77g)** **相信结合视频在看本篇题解,更有助于大家对本题的理解。** 看完本篇大家可以使用迭代法,再重新解决如下三道leetcode上的题目: * [144.二叉树的前序遍历](https://leetcode.cn/problems/binary-tree-preorder-traversal/) * [94.二叉树的中序遍历](https://leetcode.cn/problems/binary-tree-inorder-traversal/) * [145.二叉树的后序遍历](https://leetcode.cn/problems/binary-tree-postorder-traversal/) ## 思路 为什么可以用迭代法(非递归的方式)来实现二叉树的前后中序遍历呢? 我们在[栈与队列:匹配问题都是栈的强项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)中提到了,**递归的实现就是:每一次递归调用都会把函数的局部变量、参数值和返回地址等压入调用栈中**,然后递归返回的时候,从栈顶弹出上一次递归的各项参数,所以这就是递归为什么可以返回上一层位置的原因。 此时大家应该知道我们用栈也可以是实现二叉树的前后中序遍历了。 ### 前序遍历(迭代法) 我们先看一下前序遍历。 前序遍历是中左右,每次先处理的是中间节点,那么先将根节点放入栈中,然后将右孩子加入栈,再加入左孩子。 为什么要先加入 右孩子,再加入左孩子呢? 因为这样出栈的时候才是中左右的顺序。 动画如下:  不难写出如下代码: (**注意代码中空节点不入栈**)class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top(); // 中
st.pop();
result.push_back(node->val);
if (node->right) st.push(node->right); // 右(空节点不入栈)
if (node->left) st.push(node->left); // 左(空节点不入栈)
}
return result;
}
};
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int> result;
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur != NULL || !st.empty()) {
if (cur != NULL) { // 指针来访问节点,访问到最底层
st.push(cur); // 将访问的节点放进栈
cur = cur->left; // 左
} else {
cur = st.top(); // 从栈里弹出的数据,就是要处理的数据(放进result数组里的数据)
st.pop();
result.push_back(cur->val); // 中
cur = cur->right; // 右
}
}
return result;
}
};
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> result;
if (root == NULL) return result;
st.push(root);
while (!st.empty()) {
TreeNode* node = st.top();
st.pop();
result.push_back(node->val);
if (node->left) st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 (空节点不入栈)
if (node->right) st.push(node->right); // 空节点不入栈
}
reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了
return result;
}
};
// 前序遍历顺序:中-左-右,入栈顺序:中-右-左
class Solution {
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if (node.right != null){
stack.push(node.right);
}
if (node.left != null){
stack.push(node.left);
}
}
return result;
}
}
// 中序遍历顺序: 左-中-右 入栈顺序: 左-右
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while (cur != null || !stack.isEmpty()){
if (cur != null){
stack.push(cur);
cur = cur.left;
}else{
cur = stack.pop();
result.add(cur.val);
cur = cur.right;
}
}
return result;
}
}
// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
class Solution {
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<>();
if (root == null){
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()){
TreeNode node = stack.pop();
result.add(node.val);
if (node.left != null){
stack.push(node.left);
}
if (node.right != null){
stack.push(node.right);
}
}
Collections.reverse(result);
return result;
}
}
# 前序遍历-迭代-LC144_二叉树的前序遍历
class Solution:
def preorderTraversal(self, root: TreeNode) -> List[int]:
# 根结点为空则返回空列表
if not root:
return []
stack = [root]
result = []
while stack:
node = stack.pop()
# 中结点先处理
result.append(node.val)
# 右孩子先入栈
if node.right:
stack.append(node.right)
# 左孩子后入栈
if node.left:
stack.append(node.left)
return result
# 中序遍历-迭代-LC94_二叉树的中序遍历
class Solution:
def inorderTraversal(self, root: TreeNode) -> List[int]:
if not root:
return []
stack = [] # 不能提前将root结点加入stack中
result = []
cur = root
while cur or stack:
# 先迭代访问最底层的左子树结点
if cur:
stack.append(cur)
cur = cur.left
# 到达最左结点后处理栈顶结点
else:
cur = stack.pop()
result.append(cur.val)
# 取栈顶元素右结点
cur = cur.right
return result
func preorderTraversal(root *TreeNode) []int {
ans := []int{}
if root == nil {
return ans
}
st := list.New()
st.PushBack(root)
for st.Len() > 0 {
node := st.Remove(st.Back()).(*TreeNode)
ans = append(ans, node.Val)
if node.Right != nil {
st.PushBack(node.Right)
}
if node.Left != nil {
st.PushBack(node.Left)
}
}
return ans
}
func postorderTraversal(root *TreeNode) []int {
ans := []int{}
if root == nil {
return ans
}
st := list.New()
st.PushBack(root)
for st.Len() > 0 {
node := st.Remove(st.Back()).(*TreeNode)
ans = append(ans, node.Val)
if node.Left != nil {
st.PushBack(node.Left)
}
if node.Right != nil {
st.PushBack(node.Right)
}
}
reverse(ans)
return ans
}
func reverse(a []int) {
l, r := 0, len(a) - 1
for l < r {
a[l], a[r] = a[r], a[l]
l, r = l+1, r-1
}
}
func inorderTraversal(root *TreeNode) []int {
ans := []int{}
if root == nil {
return ans
}
st := list.New()
cur := root
for cur != nil || st.Len() > 0 {
if cur != nil {
st.PushBack(cur)
cur = cur.Left
} else {
cur = st.Remove(st.Back()).(*TreeNode)
ans = append(ans, cur.Val)
cur = cur.Right
}
}
return ans
}
前序遍历:
// 入栈 右 -> 左
// 出栈 中 -> 左 -> 右
var preorderTraversal = function(root, res = []) {
if(!root) return res;
const stack = [root];
let cur = null;
while(stack.length) {
cur = stack.pop();
res.push(cur.val);
cur.right && stack.push(cur.right);
cur.left && stack.push(cur.left);
}
return res;
};
中序遍历:
// 入栈 左 -> 右
// 出栈 左 -> 中 -> 右
var inorderTraversal = function(root, res = []) {
const stack = [];
let cur = root;
while(stack.length || cur) {
if(cur) {
stack.push(cur);
// 左
cur = cur.left;
} else {
// --> 弹出 中
cur = stack.pop();
res.push(cur.val);
// 右
cur = cur.right;
}
};
return res;
};
后序遍历:
// 入栈 左 -> 右
// 出栈 中 -> 右 -> 左 结果翻转
var postorderTraversal = function(root, res = []) {
if (!root) return res;
const stack = [root];
let cur = null;
do {
cur = stack.pop();
res.push(cur.val);
cur.left && stack.push(cur.left);
cur.right && stack.push(cur.right);
} while(stack.length);
return res.reverse();
};
// 前序遍历(迭代法)
function preorderTraversal(root: TreeNode | null): number[] {
if (root === null) return [];
let res: number[] = [];
let helperStack: TreeNode[] = [];
let curNode: TreeNode = root;
helperStack.push(curNode);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
res.push(curNode.val);
if (curNode.right !== null) helperStack.push(curNode.right);
if (curNode.left !== null) helperStack.push(curNode.left);
}
return res;
};
// 中序遍历(迭代法)
function inorderTraversal(root: TreeNode | null): number[] {
let helperStack: TreeNode[] = [];
let res: number[] = [];
if (root === null) return res;
let curNode: TreeNode | null = root;
while (curNode !== null || helperStack.length > 0) {
if (curNode !== null) {
helperStack.push(curNode);
curNode = curNode.left;
} else {
curNode = helperStack.pop()!;
res.push(curNode.val);
curNode = curNode.right;
}
}
return res;
};
// 后序遍历(迭代法)
function postorderTraversal(root: TreeNode | null): number[] {
let helperStack: TreeNode[] = [];
let res: number[] = [];
let curNode: TreeNode;
if (root === null) return res;
helperStack.push(root);
while (helperStack.length > 0) {
curNode = helperStack.pop()!;
res.push(curNode.val);
if (curNode.left !== null) helperStack.push(curNode.left);
if (curNode.right !== null) helperStack.push(curNode.right);
}
return res.reverse();
};
// 前序遍历迭代法
func preorderTraversal(_ root: TreeNode?) -> [Int] {
var result = [Int]()
guard let root = root else { return result }
var stack = [root]
while !stack.isEmpty {
let current = stack.removeLast()
// 先右后左,这样出栈的时候才是左右顺序
if let node = current.right { // 右
stack.append(node)
}
if let node = current.left { // 左
stack.append(node)
}
result.append(current.val) // 中
}
return result
}
// 后序遍历迭代法
func postorderTraversal(_ root: TreeNode?) -> [Int] {
var result = [Int]()
guard let root = root else { return result }
var stack = [root]
while !stack.isEmpty {
let current = stack.removeLast()
// 与前序相反,即中右左,最后结果还需反转才是后序
if let node = current.left { // 左
stack.append(node)
}
if let node = current.right { // 右
stack.append(node)
}
result.append(current.val) // 中
}
return result.reversed()
}
// 中序遍历迭代法
func inorderTraversal(_ root: TreeNode?) -> [Int] {
var result = [Int]()
var stack = [TreeNode]()
var current: TreeNode! = root
while current != nil || !stack.isEmpty {
if current != nil { // 先访问到最左叶子
stack.append(current)
current = current.left // 左
} else {
current = stack.removeLast()
result.append(current.val) // 中
current = current.right // 右
}
}
return result
}
// 前序遍历(迭代法)
object Solution {
import scala.collection.mutable
def preorderTraversal(root: TreeNode): List[Int] = {
val res = mutable.ListBuffer[Int]()
if (root == null) return res.toList
// 声明一个栈,泛型为TreeNode
val stack = mutable.Stack[TreeNode]()
stack.push(root) // 先把根节点压入栈
while (!stack.isEmpty) {
var curNode = stack.pop()
res.append(curNode.value) // 先把这个值压入栈
// 如果当前节点的左右节点不为空,则入栈,先放右节点,再放左节点
if (curNode.right != null) stack.push(curNode.right)
if (curNode.left != null) stack.push(curNode.left)
}
res.toList
}
}
// 中序遍历(迭代法)
object Solution {
import scala.collection.mutable
def inorderTraversal(root: TreeNode): List[Int] = {
val res = mutable.ArrayBuffer[Int]()
if (root == null) return res.toList
val stack = mutable.Stack[TreeNode]()
var curNode = root
// 将左节点都入栈,当遍历到最左(到空)的时候,再弹出栈顶元素,加入res
// 再把栈顶元素的右节点加进来,继续下一轮遍历
while (curNode != null || !stack.isEmpty) {
if (curNode != null) {
stack.push(curNode)
curNode = curNode.left
} else {
curNode = stack.pop()
res.append(curNode.value)
curNode = curNode.right
}
}
res.toList
}
}
// 后序遍历(迭代法)
object Solution {
import scala.collection.mutable
def postorderTraversal(root: TreeNode): List[Int] = {
val res = mutable.ListBuffer[Int]()
if (root == null) return res.toList
val stack = mutable.Stack[TreeNode]()
stack.push(root)
while (!stack.isEmpty) {
val curNode = stack.pop()
res.append(curNode.value)
// 这次左节点先入栈,右节点再入栈
if(curNode.left != null) stack.push(curNode.left)
if(curNode.right != null) stack.push(curNode.right)
}
// 最后需要翻转List
res.reverse.toList
}
}
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
//前序
pub fn preorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![root];
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
res.push(node.borrow().val);
stack.push(node.borrow().right.clone());
stack.push(node.borrow().left.clone());
}
}
res
}
//中序
pub fn inorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![];
let mut node = root;
while !stack.is_empty() || node.is_some() {
while let Some(n) = node {
node = n.borrow().left.clone();
stack.push(n);
}
if let Some(n) = stack.pop() {
res.push(n.borrow().val);
node = n.borrow().right.clone();
}
}
res
}
//后序
pub fn postorder_traversal(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<i32> {
let mut res = vec![];
let mut stack = vec![root];
while !stack.is_empty() {
if let Some(node) = stack.pop().unwrap() {
res.push(node.borrow().val);
stack.push(node.borrow().left.clone());
stack.push(node.borrow().right.clone());
}
}
res.into_iter().rev().collect()
}
}
// 前序遍历
public IList<int> PreorderTraversal(TreeNode root)
{
var st = new Stack<TreeNode>();
var res = new List<int>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var node = st.Pop();
res.Add(node.val);
if (node.right != null)
st.Push(node.right);
if (node.left != null)
st.Push(node.left);
}
return res;
}
// 中序遍历
public IList<int> InorderTraversal(TreeNode root)
{
var st = new Stack<TreeNode>();
var res = new List<int>();
var cur = root;
while (st.Count != 0 || cur != null)
{
if (cur != null)
{
st.Push(cur);
cur = cur.left;
}
else
{
cur = st.Pop();
res.Add(cur.val);
cur = cur.right;
}
}
return res;
}
// 后序遍历
public IList<int> PostorderTraversal(TreeNode root)
{
var res = new List<int>();
var st = new Stack<TreeNode>();
if (root == null) return res;
st.Push(root);
while (st.Count != 0)
{
var cur = st.Pop();
res.Add(cur.val);
if (cur.left != null) st.Push(cur.left);
if (cur.right != null) st.Push(cur.right);
}
res.Reverse(0, res.Count());
return res;
}