【LeetCode】117. 填充每个节点的下一个右侧节点指针 II

117. 填充每个节点的下一个右侧节点指针 II

难度:中等

题目

给定一个二叉树:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

示例 1:

在这里插入图片描述

输入:root = [1,2,3,4,5,null,7]
输出:[1,#,2,3,#,4,5,7,#]
解释:给定二叉树如图 A 所示,你的函数应该填充它的每个 next 指针,以指向其下一个右侧节点,如图 B 所示。序列化输出按层序遍历顺序(由 next 指针连接),'#' 表示每层的末尾。

示例 2:

输入:root = []
输出:[]

提示:

  • 树中的节点数在范围 [0, 6000]
  • -100 <= Node.val <= 100

进阶:

  • 你只能使用常量级额外空间。
  • 使用递归解题也符合要求,本题中递归程序的隐式栈空间不计入额外空间复杂度

个人题解

思路:

  1. 考虑逐层遍历,考虑用一个list来充当层的概念,每次遍历当前层的时候,把下一层的节点先装到list中
  2. 则当list中没有节点时,表示所有节点遍历完毕;有节点则继续遍历,遍历时用一个指针完成next的赋值,将当前层节点串起来
class Solution {
public Node connect(Node root) {
        if (root == null) {
            return null;
        }
        ArrayList<Node> list = new ArrayList<>();
        list.add(root);
        while (!list.isEmpty()) {
            ArrayList<Node> nextList = new ArrayList<>(); // 记录下一层遍历的节点
            Node curNode = null; // 用于连接
            for (Node node : list) {
                if (node.left != null) {
                    nextList.add(node.left);
                    if (curNode != null) {
                        curNode.next = node.left;
                    }
                    curNode = node.left;
                }
                if (node.right != null) {
                    nextList.add(node.right);
                    if (curNode != null) {
                        curNode.next = node.right;
                    }
                    curNode = node.right;
                }
            }
            list = nextList;
        }
        return root;
    }
}

进阶:

  1. 上面层的概念是用list来表示的,分析一下,
    • 如果用一个指针指向下一层的头节点,即可得到每层遍历的起点
    • 再考虑用一个尾指针表示下一层的尾节点,则遍历当前层时即可将下一层的节点接在尾节点上
  2. 经过上述分析,遍历过程不再需要list容器,只需要3个指针即可,当前层遍历指针,下一层头指针及下一层尾指针
  3. 每次遍历完当前层,将下一层头指针及尾指针重置
class Solution {
    public Node connect(Node root) {
        Node curTail = root;
        Node nextHead = null;
        Node nextTail = null;
        while (curTail != null) {
            // 看左子结点
            if (curTail.left != null) {
                if (nextTail != null) {
                    nextTail.next = curTail.left;
                } else {
                    nextHead = curTail.left;
                }
                nextTail = curTail.left;
            }
            // 看右子结点
            if (curTail.right != null) {
                if (nextTail != null) {
                    nextTail.next = curTail.right;
                } else {
                    nextHead = curTail.right;
                }
                nextTail = curTail.right;
            }
            if (curTail.next != null) {
                // 继续当前层遍历
                curTail = curTail.next;
            } else {
                // 当前层遍历完毕,开启下一层遍历,将下一层指针重置
                curTail = nextHead;
                nextHead = null;
                nextTail = null;
            }
        }
        return root;
    }
}

其他非官方题解:

class Solution {
    public Node connect(Node root) {
        Node cur = root;
        while (cur != null) {
            Node dummy = new Node(0);
            Node p = dummy;
            while (cur != null) {
                if (cur.left != null) {
                    p.next = cur.left;
                    p = p.next;
                }
                if (cur.right != null) {
                    p.next = cur.right;
                    p = p.next;
                }
                cur = cur.next;
            }
            cur = dummy.next;
        }
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值