Java Two fork Tree traversal

Source: Internet
Author: User

Package com.lever;

Import java.util.LinkedList;
Import Java.util.Queue;

/**
* Binary Tree traversal
* @author Lckxxy
*
*/
public class Node {

public int value;
Public Node left;
public Node right;

public Node (int v) {
This.value = v;
This.left = null;
This.right = null;
}

public static void Main (string[] args) {

StringBuffer str = new StringBuffer ();
Str.append ("Hello World");
String test = str.tostring ();
System.out.println (Test.replace ("", "%20"));

Node N1 = new node (1);
Node N2 = new node (2);
node N3 = new node (3);
Node N4 = new node (4);
Node N5 = new node (5);
Node N6 = new node (6);
Node N7 = new node (7);
Node N8 = new node (8);
Node N9 = new node (9);
Node N10 = new node (10);
N1.left = n2;
N1.right = n3;
N2.left = N4;
N2.right = N5;
N3.left = N6;
N3.right = N7;
N4.left = N8;
N4.right = N9;
N5.left = N10;
Leveltravel (N1);
}

/**
*
* @param root
* Tree root node
* Sequence traversal binary tree, with the queue implementation, first put the root node into the queue, as long as the queue is not empty, and then out of the queue, and access, followed by the Access node's left and right sub-tree into the queue
*/
public static void Leveltravel (Node root) {

if (root = = null)
Return
The last element of the current row
Node last = root;
The last element of the next line
Node Nlast = Root.right = = null? Root.left:root.right;
Queue q = new LinkedList ();
Q.add (root);
while (!q.isempty ()) {
Node temp = (node) q.poll ();
System.out.print (Temp.value + "");
if (temp.left! = null) {
Q.add (Temp.left);
If the left son is not empty, leave Nlast as the left son, so that Nlast is always the newest one in the current row.
Nlast = Temp.left;
}

if (temp.right! = null) {
Q.add (Temp.right);
If the right son is not empty, put nlast as the right son.
Nlast = Temp.right;
}
If the current node is the same as last, it means that it wraps, outputs a newline character, and assigns the Nlast
if (Temp.equals (last)) {
System.out.println ();
last = Nlast;
}
}
}
}

Java Two fork Tree traversal

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.