二叉树的递归实现(先,中,后)

package erchashu;
import java.util.ArrayList;
import java.util.List;


public class diguierchashu {
    public static List<TreeNode> list1 =new ArrayList<TreeNode>();
    public static List<TreeNode> list2 =new ArrayList<TreeNode>();
    public static List<TreeNode> list3 =new ArrayList<TreeNode>();
	public static void main(String[] args) {
		  TreeNode l1 =new TreeNode(1);
		  TreeNode l2 =new TreeNode(2);
		  TreeNode l3 =new TreeNode(3);
		  TreeNode l4 =new TreeNode(4);
		  TreeNode l5 =new TreeNode(5);
		  TreeNode l6 =new TreeNode(6);
		  TreeNode l7 =new TreeNode(7);
		  l1.left =l2;
		  l1.right =l3;
		  l2.left =l4;
		  l2.right =l5;
		  l3.left =l6;
		  l3.right =l7;
		  List<List<TreeNode>> list =new ArrayList<List<TreeNode>>();
          diguierchashu.convert(l1);
          list.add(list1);
          list.add(list2);
          list.add(list3);
          for(int i=0;i<list.size();i++){
        	   for(int j=0;j<list.get(i).size();j++){
        		   System.out.print(list.get(i).get(j).val);
        	   }
        	   System.out.println("^_^");
          }
	}
	 public static void convert(TreeNode root) {
            xianxu(root);
	        zhongxu(root); 
	        houxu(root);
	 }
	 
	 public static void houxu(TreeNode root) {
     		if(root == null){
     			return ;
     		}
     		houxu(root.left);
     		houxu(root.right);
     		list3.add(root);
	}
	public static void zhongxu(TreeNode root) {
            if(root == null){		
            	return ;
            }
            zhongxu(root.left);
            list2.add(root);
            zhongxu(root.right);
	}
	public static void xianxu(TreeNode root) {
            if(root == null){
            	return ;
            }
            list1.add(root);
            xianxu(root.left);
            xianxu(root.right);
	}
	 


	static class TreeNode{
		 int val = 0;
		 TreeNode left = null;
		 TreeNode right = null;
		 TreeNode(int val){
			 this.val = val;
		 }
	 }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值