A little bit of experience of binary tree

A little bit of experience of Leetcode binary tree problem

Recently, I have been brushing Leetcode's binary tree questions, so I want to write a blog to talk about my experience.
Most of the problems currently achieved can be solved by tree traversal.
The tree traversal, I think, can be roughly divided into three categories:

  1. Recursion
  2. Stack
  3. Morris traversal

In fact, the first two are very familiar to everyone, and the three types of tree traversal can be achieved through recursion or stack.
Morris traversal is actually an operation with very low space complexity, which makes full use of the free pointers of leaf nodes. You can refer to this article for details.
Then there are some skills to do the questions:

  • Pruning:
    I generally use a global variable to perform pruning operations to avoid unnecessary function calls, thereby saving running time.
  • The important feature of the binary tree:
    This is actually not a problem-solving skill, but when doing the problem, you must always remind yourself that a node of the binary tree must be greater than all the nodes on its left; it must be smaller than all the nodes on its right. Therefore, in some questions about whether to meet the characteristics of the binary tree, the root node can be used as the upper/lower bound of the child node to simplify the algorithm. Such as interview questions 04.05 and so on.

The harm, that's probably it. I haven't written a blog for a long time and my speech organization ability has declined. I hope you can understand it. . .

Guess you like

Origin blog.csdn.net/LordTech/article/details/106340678