剑指offer(第二版)——二叉树的下一个节点

PS:《剑指offer》是很多同学找工作都会参考的一本面试指南,同时也是一本算法指南(为什么它这么受欢迎,主要应该是其提供了一个循序渐进的优化解法,这点我觉得十分友好)。现在很多互联网的算法面试题基本上可以在这里找到影子,为了以后方便参考与回顾,现将书中例题用Java实现(第二版),欢迎各位同学一起交流进步。

GitHub: https://github.com/Uplpw/SwordOffer

剑指offer完整题目链接: https://blog.csdn.net/qq_41866626/article/details/120415258

1 题目描述

给定二叉树和其中一个节点(唯一参数),找到中序遍历序列的下一个节点。树中的节点除了有左右孩子指针,还有一个指向父节点的指针。

2 测试用例

一般是考虑功能用例,特殊(边缘)用例或者是反例,无效测试用例这三种情况。甚至可以从测试用例寻找一些规律解决问题,同时也可以让我们的程序更加完整鲁棒。

(1)功能用例:完全与非完全二叉树。

(2)边缘用例:只有一个节点,只有左节点,只有右节点。

(3)无效用例:树为空。

3 思路

分析:

假设二叉树如下:

// 测试用例使用的树
//            1
//         //   \\
//        2       3
//     //  \\   // \\
//     4    5   6   7
//        // \\
//		  8   9	

找中序遍历的下一个节点,中序遍历过程是:左节点——根节点——右节点,因此我们可以分析不同情况下的结果:

(1) 如果输入的当前节点有右孩子(说明当前节点是根节点),则它的下一个节点即为该右孩子为根节点的子树的最左边的节点

(2) 如果输入的当前节点没有右孩子,就需要判断其与自身父节点的关系:

  • 如果当前节点没有父节点,那所求的下一个节点不存在,返回null.
  • 如果输入节点是他父节点的左孩子,那他的父节点就是所求的下一个节点,比如4->2
  • 如果输入节点是他父节点的右孩子,那就需要将输入节点的父节点作为新的当前节点(根据中序遍历规律,需要往上找),返回到(2),判断新的当前节点与他自身父节点的关系,比如5->1

4 代码

二叉树结构类:

public class TreeNodeWithParent {
    public int val;
    public TreeNodeWithParent left;
    public TreeNodeWithParent right;
    public TreeNodeWithParent father;

    public TreeNodeWithParent(int val) {
        this.val = val;
        this.left = null;
        this.right = null;
        this.father = null;
    }
}

算法实现:

public class NextNodeInBinaryTrees {
    public static TreeNodeWithParent getNext(TreeNodeWithParent node) {
        if (node == null) {
            return null;
        }

        // 当前节点有右孩子
        if (node.right != null) {
            TreeNodeWithParent next_node = node.right;
            while (next_node.left != null) {
                next_node = next_node.left;
            }
            return next_node;
        }

        // 没有右孩子,分为几种情况
        while (true) {
            // 没有父节点
            if (node.father == null) {
                return null;
            }
            // 当前节点是父节点的左节点
            if (node.father.left == node) {
                return node.father;
            }
            // 当前节点是父节点的右节点
            if (node.father.right == node) {
                node = node.father;
            }
        }
    }

    public static void main(String[] args) {
        TreeNodeWithParent root = new TreeNodeWithParent(1);

        root.left = new TreeNodeWithParent(2);
        root.left.father = root;

        root.right = new TreeNodeWithParent(3);
        root.right.father = root;

        root.left.left = new TreeNodeWithParent(4);
        root.left.left.father = root.left;

        root.left.right = new TreeNodeWithParent(5);
        root.left.right.father = root.left;

        root.right.left = new TreeNodeWithParent(6);
        root.right.left.father = root.right;

        root.right.right = new TreeNodeWithParent(7);
        root.right.right.father = root.right;

        root.left.right.left = new TreeNodeWithParent(8);
        root.left.right.left.father = root.left.right;

        root.left.right.right = new TreeNodeWithParent(9);
        root.left.right.right.father = root.left.right;

        System.out.println(root.left.val + "->" + getNext(root.left).val); // 2->8

        System.out.println(root.val + "->" + getNext(root).val); // 1->6

        System.out.println(root.left.right.right.val + "->" + getNext(root.left.right.right).val); // 9->1

        System.out.println(root.right.right.val + "->" + getNext(root.right.right)); // 7->nullW
    }
}

参考
在解决本书例题时,参考了一些大佬的题解,比如leetcode上的官方、K神,以及其他的博客,在之后的每个例题详解后都会给出参考的思路或者代码链接,同学们都可以点进去看看!

本例题参考:

  1. https://www.jianshu.com/p/85eea9f1adf0

本文如有什么不足或不对的地方,欢迎大家批评指正,最后希望能和大家一起交流进步、拿到心仪的 offer !!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值