分治法——树的遍历(Divide and Conquer - Tree Traversal)
二叉树概念(Binary Tree Concepts)
This is a binary tree with height h = 4. 口 represents empty tree with height h = -1. 口 is an external node only at level h an h+1. Other nodes are internal nodes. Obviously, the number of external nodes is always one greater than the number of internal nodes.
Here is a full binary tree. Each node has 0 or two nodes.
Here is a complete binary tree. Each level filled nodes from left to right.
A non-empty binary tree has a root Troot, a left subtree Tleft and right subtree Tright.
计算树的高度伪代码(Computing height pseudocode)
function Height(T)
if T is empty then
return -1
else
return max{Height(Tleft), Height(Tright)}+1
二叉树的遍历(Binary Tree Traversal)
Pre-order traversal visits the root first, then the left subtree, the right subtree finally.
In-order traversal visits the left subtree first, then the root, the right subtree finally.
Post-order traversal visits the left subtree first, then the right subtree, the root finally.
Level-order traversal visits nodes level by level, starting from the root.
Visit order for the example: 17, 33, 48, 19, 16, 11, 14, 38, 31.
写在最后的话(PS)
We pointed out four tree traversal methods. There are also BFS and DFS. We talk about in other chapter.
Welcome questions always and forever.