三种遍历方式仅存在遍历节点顺序的不同而已
1、前序遍历 preOrder 遍历顺序:根节点->左子树->右子树
2、中序遍历 inOrder 遍历顺序:左子树->根节点->右子树
3、后序遍历 afterOrder 遍历顺序:左子树->右子树-->根节点
二叉树如图:
前序遍历:1 24 35
中序遍历:42 1 35
后序遍历:42 53 1
此处通过lua中的表实现二叉树结构:
local root = {
[1] = {left = 2, right = 3, value = 1},
[2] = {left = 4, value = 2},
[3] = {right = 5, value = 3},
[4] = {value = 4},
[5] = {value = 5},
}
一、递归 recursion
前序遍历 preOrder 根节点->左子树->右子树
-- 1、递归 recursion
local function preOrder_recursion(root)
-- body
local ret = {}
local function preOrder(node, ret)
-- body
if not node then
return ret
end
ret[#ret+1] = node.value
preOrder(root[node.left], ret)
preOrder(root[node.right], ret)
return ret
end
preOrder(root[1], ret)
return ret
end
中序遍历 inOrder 左子树->根节点->右子树
-- 1、递归 recursion
local function inOrder_recursion(root)
-- body
local ret = {}
local function inOrder(node, ret)
-- body
if not node then
return ret
end
inOrder(root[node.left], ret)
ret[#ret+1] = node.value
inOrder(root[node.right], ret)
return ret
end
inOrder(root[1], ret)
return ret
end
后序遍历 afterOrder 左子树->右子树-->根节点
-- 1、递归 recursion
local function afterOrder_recursion(root)
-- body
local ret = {}
local function afterOrder(node, ret)
-- body
if not node then
return ret
end
afterOrder(root[node.left], ret)
afterOrder(root[node.right], ret)
ret[#ret+1] = node.value
return ret
end
afterOrder(root[1], ret)
return ret
end
二、迭代 iteration
前序遍历 preOrder 根节点->左子树->右子树
-- 2、迭代 iteration
local function preOrder_iteration(root)
-- body
local ret, stack = {}, {1}
if not root then
return ret
end
while next(stack) do
local node = root[stack[#stack]]
if #stack == 1 then
stack = {}
else
stack[#stack] = nil
end
ret[#ret+1] = node.value
if node.right then
stack[#stack+1] = node.right
end
if node.left then
stack[#stack+1] = node.left
end
end
return ret
end
中序遍历 inOrder 左子树->根节点->右子树
-- 2、迭代 iteration
local function inOrder_iteration(root)
-- body
local ret, stack = {}, {}
if not root then return ret end
local node = root[1]
while node ~= nil or #stack~=0 do
while node ~= nil do
stack[#stack+1] = node.value
node = root[node.left]
end
node = root[stack[#stack]]
if #stack == 1 then
stack = {}
else
stack[#stack] = nil
end
ret[#ret+1] = node.value
node = root[node.right]
end
return ret
end
后序遍历 afterOrder 左子树->右子树-->根节点
-- 2、迭代 iteration 逆向思维
local function afterOrder_iteration(root)
-- body
local ret, stack = {}, {1}
if not root then return ret end
while #stack~=0 do
-- 反向求解 逆序压栈
local node = root[stack[#stack]]
ret[#ret+1] = node.value
if #stack == 1 then
stack = {}
else
stack[#stack] = nil
end
if root[node.left] ~= nil then stack[#stack+1] = root[node.left].value end
if root[node.right] ~= nil then stack[#stack+1] = root[node.right].value end
end
local function BackOrder_Tab(a)
-- body
local b = {}
for k=1,#a do
b[k] = a[#a - k + 1]
end
return b
end
-- 逆序输出
return BackOrder_Tab(ret)
end
复杂度分析
时间复杂度:
递归:O(n)
迭代:O(n)
空间复杂度:
递归:最坏O(n),平均O(log n)
迭代:O(n)