算法test007
二叉树遍历(采用中序)
方法一
二叉树节点
public class Node {
public Node left;
public Node right;
public int val;
public Node(int data){ this.val = data; }
}
递归
public static void inOrderRecursion(Node head){
if(head == null){
System.out.println("二叉树为空");
return;
}
if(head.left != null){
inOrderRecursion(head.left);
}
System.out.print(head.val+", ");
if(head.right != null){
inOrderRecursion(head.right);
}
}
非递归
public static void inOrderRecursion(Node head){
if(head==null){
System.out.println("二叉树为空");
return;
}
Stack<node> s = new Stack<node>();
while(!s.isEmpty()||head!=null){
if(head!=null){
s.push(head);
head=head.left;
}else{
head=s.pop();
System.out.print(head.val+" ");
head=head.right;
}
}
}</node></node>
欢迎交流指正~
根据自己所见所闻进行算法归纳总结