Preorder, inorder, and postorder traversal of binary tree (you can understand it with the graphic example package)

Preorder, inorder, and postorder traversal of binary tree

The following traversal takes the following binary tree as an example
insert image description here
. The code (Java) corresponding to each node is as follows:

Class BinaryTree{
    
    
	private int val;
	private BinaryTree left;
	private BinaryTree right;
}

preorder traversal

Thought

Preorder traversal, that is, traverse the root node first, and then traverse the left and right nodes.
insert image description here
Then the corresponding traversal order should be 1->4->3->2->5

walk through the code

	//前序遍历
    public static void preScan(TreeNode root){
    
    
        System.out.println(root.val);
        if(root.left != null){
    
    
            preScan(root.left);
        }
        if(root.right != null){
    
    
            preScan(root.right);
        }
    }

Code explanation: Pass the root node into the method, first print the value of the root node, then traverse the left subtree to the left, and then traverse the right subtree to the right.
End the traversal until the left and right nodes of the current node are empty.

Inorder traversal

Thought

In-order traversal, that is, traverse the left node first, then the middle node, and finally the right node.
insert image description here
Then the corresponding traversal order should be 3->4->1->5->2

walk through the code

	//中序遍历
    public static void midScan(TreeNode root){
    
    
        if(root.left != null){
    
    
            midScan(root.left);
        }
        System.out.println(root.val);
        if(root.right != null){
    
    
            midScan(root.right);
        }
    }

Code explanation: Pass the root node into the method, first traverse the left subtree to the left, then print the value of the root node, and then traverse the right subtree to the right.
End the traversal until the left and right nodes of the current node are empty.

post-order traversal

Thought

Post-order traversal, first traverse the left node, then traverse the right node, and finally traverse the middle node.
insert image description here
Then the corresponding traversal order should be 3->4->5->2->1

walk through the code

	//后序遍历
    public static void lastScan(TreeNode root){
    
    
        if(root.left != null){
    
    
            lastScan(root.left);
        }
        if(root.right != null){
    
    
            lastScan(root.right);
        }
        System.out.println(root.val);
    }

Code explanation: Pass the root node into the method, first traverse the left subtree to the left, then traverse the right subtree to the right, and then print the value of the root node.
End the traversal until the left and right nodes of the current node are empty.

LeetCode Simple Questions

insert image description here
Ideas :

The review found that the given binary tree is a search tree, that is, the left child nodes are all smaller than the parent node, and the right child nodes are all larger than the parent node.
It is found that the in-order traversal of this binary tree, the obtained values ​​are arranged in order from small to large.

Then this is easy to do. We can create a List, put the value of the result of the in-order traversal into the list, and the result is ordered, then we can get the largest node according to the subscript.

code show as below:

	public static int kthLargest(TreeNode root, int k) {
    
    
        ArrayList<Integer> list = new ArrayList<>();
        if(root != null){
    
    
            midGet(root, list);
        }
        //因为列表是升序的,这里用list的长度减去k,就刚好是第k大的数。如果不能理解,你可以把list先反转一下,就是倒序了,那么第k大的就是k-1(因为list下标从0开始嘛)。
        return list.get(list.size() - k); 
    }
    public static void midGet(TreeNode t, List<Integer> list){
    
    
        if(t.left != null){
    
    
            midGet(t.left, list);
        }
        list.add(t.val);
        if(t.right != null){
    
    
            midGet(t.right, list);
        }
    }

Summarize

  • The front, middle and back traversal of a binary tree is actually quite simple. Just remember that the front, middle and back are all the root nodes. That is to say, pre-order traversal means traversing the root first, in-order means traversing the root in the middle, and post-order means traversing the root last. . In addition, the left and right are traversed first.
  • Through an actual algorithm problem, I believe that the little friends have a firmer grasp of the binary tree traversal and improved a little bit.

Guess you like

Origin blog.csdn.net/qq_41570752/article/details/108569204