【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】

126 篇文章 82 订阅

【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】


【LeetCode-面试算法经典-Java实现】【所有题目目录索引】

原题

  Follow up for problem “Populating Next Right Pointers in Each Node”.
  What if the given tree could be any binary tree? Would your previous solution still work?
  Note:
  You may only use constant extra space.
  For example,
  Given the following binary tree,

         1
       /  \
      2    3
     / \    \
    4   5    7

  After calling your function, the tree should look like:

         1 -> NULL
       /  \
      2 -> 3 -> NULL
     / \    \
    4-> 5 -> 7 -> NULL

题目大意

  给定一棵二叉树,有一个next指针,将它们的每一层链接起来。只能使用常量额外空间,树是一棵任意的二叉树。

解题思路

  将树的每一层节点用next串起来。这样每一层也会形成一个单链表。而每层的链表头,则是,根的左孩子,左孩子,左孩子。利用双循环,外层循环,沿着根的左孩子,一直向下。内层循环,负责将下一层的节点串起来。即,将自己右孩子放到左孩子的next上,而右孩子,则可通过自己的next指针,找到右邻居。

代码实现

树结点类

public class TreeLinkNode {
    TreeLinkNode left;
    TreeLinkNode right;
    TreeLinkNode next;
}

算法实现类,使用常量空间

public class Solution {
    public void connect(TreeLinkNode root) {
        TreeLinkNode queue = root;
        TreeLinkNode level = new TreeLinkNode(0);
        while (queue != null) {
            level.next = null;
            TreeLinkNode current = level;
            while (queue != null) {
                if (queue.left != null) {
                    current.next = queue.left;
                    current = current.next;
                }
                if (queue.right != null) {
                    current.next = queue.right;
                    current = current.next;
                }
                queue = queue.next;
            }
            queue = level.next;
        }
    }
}

算法实现类,使用非常量空间

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Solution {

    public void connect(TreeLinkNode root) {
        if (root != null) {
            // 保存结点
            List<TreeLinkNode> list = new LinkedList<>();
            // 当前处理的结点的前一个结点
            TreeLinkNode prev = null;
            // 当前处理的结点
            TreeLinkNode node;
            // 当前层剩余的结点个数
            int curr = 1;
            // 记录下一层的元素个数
            int next = 0;

            // 根结点入队
            list.add(root);

            // 队列非空
            while (list.size() > 0) {
                // 删除队首元素
                node = list.remove(0);
                // 当前层剩余数减少
                curr--;

                // 左子树非空,左子结点入队
                if (node.left != null) {
                    list.add(node.left);
                    next++;
                }

                // 右子树非空,右子结点入队
                if (node.right != null) {
                    list.add(node.right);
                    next++;
                }

                // 如果当前层处理完了
                if (curr == 0) {

                    // 对下一层的元素进行串接
                    Iterator<TreeLinkNode> iterable = list.iterator();
                    if (iterable.hasNext()) {
                        prev = iterable.next();
                        while (iterable.hasNext()) {
                            node = iterable.next();
                            prev.next = node;
                            prev = node;
                        }
                    }

                    // 更新当前层剩余的结点数
                    curr = next;
                    // 重新统计下层结点数
                    next = 0;
                }
            }
        }
    }
}

评测结果

  点击图片,鼠标不释放,拖动一段位置,释放后在新的窗口中查看完整图片。

  使用常量空间

这里写图片描述

  使用非常量空间

这里写图片描述

特别说明

欢迎转载,转载请注明出处【http://blog.csdn.net/derrantcm/article/details/47588657

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值