Java实现二叉树的递归、非递归,前序遍历、中序遍历、后序遍历;以及层次遍历

Java实现二叉树的递归、非递归,前序遍历、中序遍历、后序遍历;以及层次遍历

递归前序遍历

ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
	if (root != null) {
            list.add(root.val);
            preOrder(root.left);
            preOrder(root.right);
    }
    return list;
}	            

非递归前序遍历

	ArrayList<Integer> list = new ArrayList();
    public ArrayList<Integer> preOrder2(TreeNode root) {
        Stack<TreeNode> stack = new Stack();
        while (root != null || !stack.empty()) {
            while (root != null) {
                stack.push(root);
                list.add(root.val);
                root = root.left;
            }
            if (!stack.empty()) {
                root = stack.pop();
                root = root.right;
            }
        }
        return list;
    }

递归中序遍历

ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
	if (root != null) {
            preOrder(root.left);
        	list.add(root.val);
            preOrder(root.right);
    }
    return list;
}	

非递归中序遍历

	ArrayList<Integer> list = new ArrayList();
    public ArrayList<Integer> preOrder2(TreeNode root) {
        Stack<TreeNode> stack = new Stack();
        while (root != null || !stack.empty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            if (!stack.empty()) {
                root = stack.pop();
                list.add(root.val);
                root = root.right;
            }
        }
        return list;
    }

递归后序遍历

ArrayList<Integer> list = new ArrayList();
public ArrayList<Integer> preOrder(TreeNode root) {
	if (root != null) {
            preOrder(root.left);
            preOrder(root.right);
        	list.add(root.val);
    }
    return list;
}

非递归后序遍历

public ArrayList<Integer> preOrder2(TreeNode root) {
        ArrayList<Integer> list = new ArrayList();
        Stack<TreeNode> stack = new Stack();
        TreeNode r = null;
        while (root != null || stack.empty()) {
            if (root != null) {
                stack.push(root);
                root = root.left;
            } else {
                root = stack.peek();
                if (root.right != null && root.right != r) {
                    root = root.right;
                    stack.push(root);
                    root = root.left;
                } else {
                    root = stack.pop();
                    list.add(root.val);
                    r = root;
                    root = null;
                }
            }
        }
        return list;
    }

层次遍历

public ArrayList<Integer> preOrder2(TreeNode root) {
        ArrayList<Integer> list = new ArrayList();
        Queue<TreeNode> queue = new LinkedList();
        queue.add(root);
        while (root != null || !queue.isEmpty()) {
            TreeNode node = queue.poll();
            list.add(node.val);
            if (node.left != null) {
                queue.add(node.left);
            }
            if (node.right != null) {
                queue.add(node.right);
            }
        }
        return list;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值