weixin_47019501 2021-04-03 12:08 采纳率: 50%
浏览 29
已采纳

这个怎么改为非递归形式实现?

import java.util.Arrays;
import java.util.Scanner;


public class Main {
	
	private static class TreeNode {  
		TreeNode left;  
		TreeNode right;  
        int data;  
  
        TreeNode(int newData) {  
            left = null;  
            right = null;  
            data = newData;  
        }  
    }  
	
    public static void main(String[] args) {
    	Scanner input=new Scanner(System.in);
    	int n=input.nextInt();
    	int[] pre =new int[n];
    	for(int i=0;i<n;i++)
    		pre[i] =input.nextInt();
    	int[] in =new int[n];
    	for(int i=0;i<n;i++)
    		in[i] =input.nextInt();
    	input.close();
        TreeNode treeNode = reConstructBinaryTree(pre, in);
        prePost(treeNode);

    }
     
    
    public static TreeNode reConstructBinaryTree(int [] pre,int [] in) {
    	
        if(pre.length == 0 || in.length == 0)
            return null;
        TreeNode root = new TreeNode(pre[0]);
        for (int i = 0; i < in.length; i++) {
            if(in[i] == pre[0]){
                //左子树,copyOfRange函数是左闭右开
                root.left = reConstructBinaryTree(Arrays.copyOfRange(pre,1,i+1),Arrays.copyOfRange(in,0,i));
                //递归构建左子树
                root.right = reConstructBinaryTree(Arrays.copyOfRange(pre,i+1,pre.length),Arrays.copyOfRange(in,i+1,in.length));
                break;
            }
        }
        return root;
    }
   
   
    public static void prePost(TreeNode root){
        if(root == null)
            System.out.println("空树");
        

        if(root.left != null)
            prePost(root.left);

        if(root.right!= null)
            prePost(root.right);
        System.out.print(root.data+" ");
    }
}
  • 写回答

3条回答 默认 最新

  • allway2 2021-04-03 20:10
    关注
    
    import java.util.Arrays;
    import java.util.Deque;
    import java.util.LinkedList;
    import java.util.Scanner;
    
    public class Main2 {
    
    	private static class TreeNode {
    
    		TreeNode left;
    
    		TreeNode right;
    
    		int data;
    
    		TreeNode(int newData) {
    
    			left = null;
    
    			right = null;
    
    			data = newData;
    
    		}
    
    	}
    
    	public static void main(String[] args) {
    
    		Scanner input = new Scanner(System.in);
    
    		int n = input.nextInt();
    
    		int[] pre = new int[n];
    
    		for (int i = 0; i < n; i++)
    
    			pre[i] = input.nextInt();
    
    		int[] in = new int[n];
    
    		for (int i = 0; i < n; i++)
    
    			in[i] = input.nextInt();
    
    		input.close();
    
    		TreeNode treeNode = reConstructBinaryTree(pre, in);
    
    		prePost(treeNode);
    
    	}
    
    	public static TreeNode reConstructBinaryTree(int[] pre, int[] in) {
    
    		if (pre == null || pre.length == 0) {
    			return null;
    		}
    		TreeNode root = new TreeNode(pre[0]);
    		Deque<TreeNode> stack = new LinkedList<TreeNode>();
    		stack.push(root);
    		int inorderIndex = 0;
    		for (int i = 1; i < pre.length; i++) {
    			int preorderVal = pre[i];
    			TreeNode node = stack.peek();
    			if (node.data != in[inorderIndex]) {
    				node.left = new TreeNode(preorderVal);
    				stack.push(node.left);
    			} else {
    				while (!stack.isEmpty() && stack.peek().data == in[inorderIndex]) {
    					node = stack.pop();
    					inorderIndex++;
    				}
    				node.right = new TreeNode(preorderVal);
    				stack.push(node.right);
    			}
    		}
    		return root;
    	}
    
    	public static void prePost(TreeNode root) {
    
    		if (root == null)
    
    			System.out.println("空树");
    
    		if (root.left != null)
    
    			prePost(root.left);
    
    		if (root.right != null)
    
    			prePost(root.right);
    
    		System.out.print(root.data + " ");
    
    	}
    
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条