Tree-related knowledge points--scattered notes

In-depth understanding of the front, middle and back sequence

  1. What is the front, middle and back order traversal of a binary tree?

    1. Front, middle and back order traversal, that is, the front, middle and back positions of the binary tree structure
      1. Preorder traversal - that is, when just entering a node
      2. In-order traversal - that is, before leaving the node after entering the node
      3. Post-order traversal - that is, when leaving the first node
    2. The front, middle and back order are three special time points for processing each node in the process of traversing the binary tree
      1. Before – execute when just entering a binary tree node
      2. After – Executed when a binary tree node is about to be left
      3. Middle – Executed when the left subtree of a binary tree has been traversed and the right subtree is about to be traversed
  2. What is special about postorder traversal?

    1. The code at the preorder position can only obtain the data passed by the parent node from the function parameters
    2. The code in the subsequent position can not only obtain the parameter data, but also obtain the data returned by the subtree through the return value of the function
      1.
  3. Why is there no inorder traversal of a multi-fork tree?

    1. Each node in the binary tree will only switch the left and right subtrees once
    2. A multi-fork tree may have many child nodes, and the subtree will be switched multiple times to traverse
    3. Therefore, multi-tree nodes do not have a unique in-order traversal position

The nature of the binary tree problem

All the problems with the binary tree are to inject ingenious code logic into the front, middle and back order positions to achieve the goal. Therefore, you only need to think about what each node should do separately, and the rest is handed over to the binary tree traversal framework, and the recursion will do the same operation on all nodes.

problem solving ideas

  1. The recursive solution to the binary tree problem can be divided into two types of ideas
    1. The first category: traverse the binary tree to get the answer, that is, the core framework of the backtracking algorithm
    2. The second category: calculate the answer by decomposing the problem, that is, the core framework of dynamic programming
  2. Is it possible to get the answer by traversing the binary tree once
  3. Is it possible to define a recursive function to deduce the answer to the original question through the answer to the sub-question (subtree)?
    1. If you can, think carefully about how to define recursive functions

The importance of inorder traversal

  1. Inorder traversal traverses in left-root-right order
  2. The traversed data has a non-decreasing nature, which is equivalent to traversing an ordered array

Special features of postorder traversal

  1. The code at the preorder position can only obtain the data passed by the parent node from the function parameters
  2. The post-sequence position code can not only obtain parameter data, but also obtain the data passed back by the subtree through the return value
  3. Instructions for using examples
    1. If the root node is regarded as the first layer, print out the layer number of each node?
void traverse(TreeNode root, int level) {
    
    
    if(root == null) {
    
    
        return;
    }

    // 前序位置
    System.out.println("节点 " + root + " 在,第 " + level + " 层");

    traverse(root.left, level + 1);
    traverse(root.right, level + 1);
}

// 调用
traverse(root, 1);
  1. How to print the number of nodes in the left and right subtrees of each node?
int count(TreeNode root) {
    
    
    if(root == null) {
    
    
        return 0;
    }
	int leftCount = count(root.left);
	int rightCount = count(root.right);

	// 后序位置
	System.out.println(leftCount);
	System.out.println(rightCount);

	return leftCount + rightCount + 1;
}
  • The difference between the two questions ab
    • Which layer a node is in can be known when traversing from the root node, because it can be recorded incidentally during the traversal process
    • How many nodes are there in the entire subtree rooted at a node? You need to traverse the subtree to know how many nodes, and get the answer through the return value of the recursive function
  • Therefore, when facing a problem, it is necessary to carefully analyze whether the problem solution is related to the sub-problem or whether the result can be obtained directly through traversal

Noteworthy points

  1. If possible, try to use functions that do not return a value as recursive functions
    1. Doing so will reduce the use of extra space
  2. Thinking about how to simplify the judgment logic will make the code more concise and easy to understand

reference

https://labuladong.github.io/algo/di-ling-zh-bfe1b/dong-ge-da-334dd/

Guess you like

Origin blog.csdn.net/GoNewWay/article/details/130787323