traverse binary tree

 Traversal operation is the basic operation of binary tree, mainly including preorder, inorder, postorder and level order traversal.
    1. The basic method of pre-order traversal: root left and right
For a non-empty binary tree, first visit the root node, then traverse the left subtree of the root preorder, and finally traverse the right subtree of the root preorder.
Therefore, if the preorder traversal sequence of a binary tree is known, its root node can be obtained directly.
   2. The basic method of in-order traversal: left root right
For a non-empty binary tree, first traverse the left subtree of the root in inorder, then visit the root node, and finally traverse the right subtree of the root in order.
Therefore, if the root node of a binary tree is known, the nodes on the left and right subtrees of the binary tree can be divided according to the in-order traversal sequence.
    3. The basic method of post-order traversal: left and right roots
For a non-empty binary tree, first traverse the left subtree of the root post-order, then traverse the right subtree of the root post-order, and finally visit the root node.
Therefore, if the post-order traversal sequence of a binary tree is known, its root node can be obtained directly.
     4. The method of traversing the binary tree in level order:
        Assuming that the level of the root node of the binary tree is 1, the operation of traversing the binary tree in level order is defined as starting from the root node of the tree, first visiting the node (root node) of the first layer, and then visiting sequentially from left to right Nodes on the second level, followed by nodes on the third level, and so on, visit the nodes on each level of the tree from top to bottom, left to right.  
  
Note: The nodes on the left subtree and the right subtree cannot be distinguished from the preorder traversal sequence and the postorder traversal sequence. Therefore, the binary tree cannot be constructed from the preorder traversal sequence and the postorder traversal sequence of a binary tree. The inorder traversal sequence of .
   

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326064492&siteId=291194637