Binary tree traversal method memory

  1. For the basics, including binary tree binary tree traversal, and sometimes pen questions may have this title previously, although relatively simple, but often do not remember for beginners may live;
  2. In simple terms there are binary tree traversal three ways, before, during and after order traversal, general recursive algorithm, and some may also exist so-called traverse the level, that is, layer by layer traversal;
  3. From https://www.cnblogs.com/songwenjie/p/8955856.html Here Pirates dpi, respectively explain 3 traversal;
  4.  

    In fact, to remember what it is also very simple, are traversed by the order according to the position of the root node, the root node is at a first preamble, the root node is the second order, the root node is the third follow-up ;

  5. Algorithm recursive call is as follows (Java version):
    import lombok.Data;
    
    @Data
    public class Node<T> {
    
        private T data;
        private Node<T> left;
        private Node<T> right;
    
        public static void main(String[] args) {
            Node<String> root = new Node<>();
            iterator(root);
        }
    
        public static <T> void iterator(Node<T> root) {
            if (root == null)
                return;
            System.err.println(root.getData());
            iterator(root.getLeft());
            iterator(root.getRight());
            
        }
    
    }
  6. 代码就是这么简单iterator中处理分别处理根,左,右三个节点,如果根节点在第一位就是前序,在第二位就是中序,在第三位就是后序,这样子一来的话面对二叉树遍历的笔试题,脑海里默念一下这个算法就很清晰了;

Guess you like

Origin www.cnblogs.com/dreamroute/p/11261024.html