17. Binary tree-level traversal-up, down, left and right and up and down, right and left

Each node of the binary tree is printed from top to bottom, and the nodes of the same level are printed from left to right (right to left).

Idea: Use a queue to store each node, and pop the first node each time you loop

, and store the left and right tree nodes of the pop-up node to the end of the tree.

////////////////////////////////////from left to right////// //////////////////////////////

package facehandjava.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.LinkedBlockingDeque;

public class L2RRecursiveBinaryTree {
   
/* *
     *
Pre-order, in-order, and post-order sorting of binary tree (non-recursive version)
     */
   
public Node init() { // Note that it must be built in reverse order, first create child nodes, and then build up in reverse order, because non-leaf nodes will use The following nodes are initialized in order, and if they are not built in reverse order, an error will be reported.
       
Node J = new Node( 8 , null , null );
        Node H =
new Node(4, null, null);
        Node G =
new Node(2, null, null);
        Node F =
new Node(7, null, J);
        Node E =
new Node(5, H, null);
        Node D =
new Node(1, null, G);
        Node C =
new Node(9, F, null);
        Node B =
new Node(3, D, E);
        Node A =
new Node(6, B, C);
       
return A;   //返回根节点
   
}

   
public static void main(String[] args) {
        L2RRecursiveBinaryTree tree =
new L2RRecursiveBinaryTree();
        Node root = tree.init();
        System.
out.println("从左到右的二叉树遍历");
       tree.L2RRecursiveBinaryTree(root);
    }


   
private void L2RRecursiveBinaryTree(Node root) {
        Queue<Node> queue =
new LinkedList<>();
       
int count= 0;
       
if (root!= null) {
            queue.add(root);
        }
       
while (!queue.isEmpty()){
            count = queue.size();
           
while (count> 0) {
                Node temp = queue.remove();
                System.
out.print(temp.getData());
                count--;
               
if (temp.getLeftNode() != null) {
                   queue.add(temp.getLeftNode());
                }
               
if (temp.getRightNode() != null) {
                    queue.add(temp.getRightNode());
                }
            }
        }
    }
}

 

////////////////////////////////////from right to left////// //////////////////////////////

package facehandjava.tree;

import java.util.LinkedList;
import java.util.Queue;

public class R2LRecursiveBinaryTree {
   
/**
     *
From right to left
     */
   
public Node init() { // Note that it must be created in reverse order, first create child nodes , and then build up in reverse order, because non-leaf nodes will use the following nodes, and the initialization is initialized in order, and if not built in reverse order, an error will be reported
       
Node J = new Node( 8 , null , null );
        Node H =
new Node ( 4 , null , null );
        Node G =
new Node( 2 , null, null );
        Node F =
new Node ( 7 , null , J);
        Node E =
new Node ( 5 , H, null );
        Node D =
new Node ( 1 , null , G);
        Node C =
new Node ( 9 ) , F, null );
        Node B =
new Node ( 3 , D, E);
        Node A =
new Node ( 6 , B, C);
       
return A;   // Return root point
   
}



   
public static void R2LRecursiveBinaryTree(Node root) {
        Queue<Node> queue =
new LinkedList<>();
        Node temp =
null;
        queue.add(root);
       
while (!queue.isEmpty()){
           
int i =queue.size();
           
while (i>0) {
                temp = queue.remove();
                i--;
                System.
out.print(temp.getData());
               
if (temp.getRightNode() != null) {
                   queue.add(temp.getRightNode());
                }
               
if (temp.getLeftNode() != null) {
                    queue.add(temp.getLeftNode());
                }
            }
        }

    }


   
public static void main(String[] args) {
        R2LRecursiveBinaryTree tree =
new R2LRecursiveBinaryTree();
        Node root = tree.init()
; System.out         .println ( " Right-to-left binary tree traversal" );
        R2LRecursiveBinaryTree (root);
    }
}

 


Guess you like

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