java+stack+for遍历,二叉树的java 实现-创建,遍历

import java.util.LinkedList;

import java.util.Queue;

import java.util.Stack;

public class TreeNode {

int val;

TreeNode left;

TreeNode right;

TreeNode(int x) { val = x; }

public static TreeNode creatBTfromArr(TreeNode node,int[] a,int pos){

if(a.length<1) return null;

if(pos<=a.length&&a[pos-1]>0){

node = new TreeNode(a[pos-1]);

node.left=creatBTfromArr(node.left, a, pos*2);

node.right=creatBTfromArr(node.right, a, pos*2+1);

return node;

}

else

return null;

}

public static void travPreOrder(TreeNode R){

if(R!=null){

System.out.print(R.val);

System.out.print(" ");

travPreOrder(R.left);

travPreOrder(R.right);

}

}

public static void travInOrder(TreeNode R){

if(R!=null){

travInOrder(R.left);

System.out.print(R.val);

System.out.print(" ");

travInOrder(R.right);

}

}

public static void travPostOrder(TreeNode R){

if(R!=null){

travPostOrder(R.left);

travPostOrder(R.right);

System.out.print(R.val);

System.out.print(" ");

}

}

public static void travLevelOrder(TreeNode R){

Queue tmp = new LinkedList<>();

if(R!=null) tmp.add(R);

while(!tmp.isEmpty()){

TreeNode ttmp=tmp.remove();

System.out.print(ttmp.val);

System.out.print(" ");

if(ttmp.left!=null)

tmp.add(ttmp.left);

if(ttmp.right!=null)

tmp.add(ttmp.right);

}

}

public static class SNode{

public TreeNode R;

public int tag;

public SNode() {

// TODO Auto-generated constructor stub

}

public SNode(TreeNode R, int tag) {

this.R =R;

this.tag = tag;

}

}

public static void travPreOrder_noRec(TreeNode R){

Stack stack = new Stack<>();

if(R!=null){

SNode tmp = new SNode(R,1);

stack.push(tmp);

System.out.print(R.val);

System.out.print(" ");

R=R.left;

}

while(!stack.isEmpty()){

while(R!=null){

stack.push(new SNode(R,1));

System.out.print(R.val);

System.out.print(" ");

R=R.left;

}

while(!stack.isEmpty()&&stack.peek().tag==2)

stack.pop();

if(!stack.isEmpty()&&stack.peek().tag==1){

R = stack.peek().R.right;

stack.peek().tag=2;

}

}

}

// 前序遍历-入栈即打印

public static void travPreOrder_noRec2(TreeNode R){

Stack stack = new Stack<>();

while(!stack.isEmpty()||R!=null){

if(R!=null){

stack.push(R);

System.out.print(R.val + " ");

R=R.left;

}

else

R=stack.pop().right;

}

}

public static void travInOrder_noRec(TreeNode R){

if(R!=null){

Stack stack = new Stack<>();

stack.push(new SNode(R,1));

R=R.left;

while(!stack.isEmpty()){

while(R!=null){

stack.push(new SNode(R,1));

R=R.left;

}

while(!stack.isEmpty()&&stack.peek().tag==2)

stack.pop();

if(!stack.isEmpty()&&stack.peek().tag==1){

System.out.print(stack.peek().R.val + " ");

R=stack.peek().R.right;

stack.peek().tag=2;

}

}

}

}

// 中序遍历-出栈即打印

public static void travInOrder_noRec2(TreeNode R){

if(R!=null){

Stack stack = new Stack<>();

while (R!=null||!stack.isEmpty()) {

if(R!=null){

stack.push(R);

R=R.left;

}

else{

R=stack.peek().right;

System.out.print(stack.pop().val + " ");

}

}

}

}

public static void travPostOrder_noRec(TreeNode R){

if(R!=null){

Stack stack = new Stack<>();

stack.push(new SNode(R,1));

R = R.left;

while(!stack.isEmpty()){

while (R!=null) {

stack.push(new SNode(R,1));

R=R.left;

}

while(!stack.isEmpty()&&stack.peek().tag==2){

System.out.print(stack.pop().R.val + " ");

}

if(!stack.isEmpty()&&stack.peek().tag==1){

R=stack.peek().R.right;

stack.peek().tag=2;

}

}

}

}

public boolean isSameTree(TreeNode p, TreeNode q) {

if(p==null && q==null) return true;

if(p==null || q==null) return false;

if(q.val==q.val){

return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);

}

else

return false;

}

public static int treeDepth(TreeNode R){

if(R==null) return 0;

else{

return(Math.max(treeDepth(R.left), treeDepth(R.right))+1);

}

}

public static int nodeNum(TreeNode R){

if(R==null) return 0;

else{

return(nodeNum(R.left)+nodeNum(R.right)+1);

}

}

public static int leafnodeNum(TreeNode R){

if(R==null) return 0;

if(R.left==null && R.right==null) return 1;

return(leafnodeNum(R.left)+leafnodeNum(R.right));

}

public static List inOrder094(TreeNode R){

List res = new ArrayList<>();

if(R==null) return res;

else{

Stack tmp = new Stack<>();

tmp.push(R);

R=R.left;

while(!tmp.isEmpty() || R!=null){

if(R!=null){

tmp.push(R);

R=R.left;

}

else{

R=tmp.peek().right;

res.add(tmp.pop().val);

}

}

return res;

}

}

public static int uniqueBSTnum(int n){

if(n<1) return 0;

else{

int[] res = new int[n+1];

res[0] = 1;

res[1] = 1;

for(int i=2;i<=n;i++){

for(int j=0;j

res[i]+=res[j]*res[i-1-j];

}

return res[n];

}

}

public static void main(String[] args){

int[] a={1,2,3,4,5,6,-1,-1,-1,7};

TreeNode node=null;

TreeNode R = creatBTfromArr(node, a, 1);

System.out.println("前序-递归:");

travPreOrder(R);

System.out.println("\n前序-非递归:");

travPreOrder_noRec(R);

System.out.println("\n前序-非递归2:");

travPreOrder_noRec2(R);

System.out.println("\n中序-递归:");

travInOrder(R);

System.out.println("\n中序-非递归:");

travInOrder_noRec(R);

System.out.println("\n中序-非递归2:");

travInOrder_noRec2(R);

System.out.println("\n后序-递归:");

travPostOrder(R);

System.out.println("\n后序-非递归:");

travPostOrder_noRec(R);

System.out.println("\n层序:");

travLevelOrder(R);

System.out.println("\n树深度:");

System.out.println(treeDepth(R));

System.out.println("树节点个数:");

System.out.println(nodeNum(R));

System.out.println("叶子节点个数:");

System.out.println(leafnodeNum(R));

List res1 = inOrder094(R);

for(int i : res1){

System.out.println(i);

}

System.out.println("uniqueBSTnum:");

System.out.println(uniqueBSTnum(5));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值