Java来实现二叉树算法,将一个二叉树左右倒置(左右孩子节点互换)


今天来和大家谈谈常用的二叉树算法

二叉树算法

二叉树左右变换数据

举个例子:
Java来实现二叉树算法,将一个二叉树左右倒置(左右孩子节点互换)如下图所示
在这里插入图片描述

实现的代码如下:以下定义的类是内部类

//    2. 讲一个做二叉树左右替换
//    1. 先给一个二叉树的内部类,类里面分出左右的对象的变量
    public static class Tree {
        private Object data;
        public Object getData() {
            return data;
        }
        public void setData(Object data) {
            this.data = data;
        }
        public Tree getLeft() {
            return left;
        }
        public void setLeft(Tree left) {
            this.left = left;
        }
        public Tree getRight() {
            return right;
        }
        public void setRight(Tree right) {
            this.right = right;
        }

        // 给出左右的树
        private Tree left;
        private Tree right;
        // 构造方法
        public Tree(Object data, Tree left, Tree right) {
            this.data = data;
            this.left = left;
            this.right = right;
        }

        /**
         * 获取二叉树的深度
         * @param root root
         * @return int
         */
        public static int getMaxDepth(Tree root) {
            if (root == null) return 0;
            // 需要递归获取深度
            return Math.max(getMaxDepth(root.left), getMaxDepth(root.right)) + 1;
        }

        /**
          * 二叉树的交换方法OK,经过验证
          * @param root Tree
         */
        public static void swap(Tree root) {
            if (root == null) return;
            // 交换
            Tree tmp = root.left;
            root.left = root.right;
            root.right = tmp;
            // 递归交换
            swap(root.left);
            swap(root.right);
        }

        /**
          *  二叉树的左右交换
          * @param root
         */
        public static void swapAgin(Tree root) {
            if (root == null) return;

            Stack<Tree> statck = new Stack<>();
            statck.push(root);
            while (!statck.isEmpty()) {
                Tree node = statck.pop();
                Tree tmp = node.left;
                node.left = node.right;
                node.right = tmp;
                if (node.left != null) {
                    statck.push(node.left);
                }

                if (node.right != null) {
                    statck.push(node.right);
                }
            }
        }

        public static void main(String[] args) {
            // 以下模拟的二叉树图片地址在此: https://img-blog.csdnimg.cn/1ae454d94ab04d8e8fb6f313248beb3a.png
            Tree left9 = new Tree(9, null, null);
            Tree right3 = new Tree(3, null, null);
            Tree right8 = new Tree(8, null, null);
            Tree left7 = new Tree(7, null, right8);
            Tree right11 = new Tree(11, left9, right3);
            Tree root5 = new Tree(5, left7, right11);
            System.out.println("原始的二叉树结果:" + root5);
//       交换
            swap(root5);
//            swapAgin(root5);
            System.out.println("swap()方法交换之后的二叉树结构为:" + root5);
        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮攻城狮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值