【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
来源:互联网 发布:json封装和解析 编辑:程序博客网 时间:2024/05/21 08:39
【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 3
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- 【LeetCode-面试算法经典-Java实现】【116-Populating Next Right Pointers in Each Node(二叉树链接右指针)】
- populating-next-right-pointers-in-each-node-ii(二叉树每层用指针链接)
- leetcode:Populating Next Right Pointers in Each Node II (顺序连接二叉树每一层节点)【面试算法题】
- Leetcode 117 Populating Next Right Pointers in Each Node II 二叉树填充next指针指向右侧结点 II
- [C++]LeetCode: 124 Populating Next Right Pointers in Each Node II(链接二叉树)
- Populating Next Right Pointers in Each Node II 任意(非完美)二叉树添加next指针 @LeetCode
- [Leetcode] Populating Next Right Pointers in Each Node II (Java)
- [LeetCode][Java] Populating Next Right Pointers in Each Node II
- leetcode:Populating Next Right Pointers in Each Node II 【Java】
- LeetCode 117 Populating Next Right Pointers in Each Node II
- LeetCode: Populating Next Right Pointers in Each Node II [117]
- LeetCode 117 Populating Next Right Pointers in Each Node II
- [leetcode 117] Populating Next Right Pointers in Each Node II
- [LeetCode 117]Populating Next Right Pointers in Each Node II
- leetcode || 117、Populating Next Right Pointers in Each Node II
- Populating Next Right Pointers in Each Node II - LeetCode 117
- leetcode[117]:Populating Next Right Pointers in Each Node II
- 银川代考托福
- 西宁代考托福
- 海口代考托福
- 洛阳代考托福
- 南通代考托福
- 【LeetCode-面试算法经典-Java实现】【117-Populating Next Right Pointers in Each Node(二叉树链接右指针II)】
- 常州代考托福
- 徐州代考托福
- 潍坊代考托福
- 淄博代考托福
- 绍兴代考托福
- 温州代考托福
- 台州代考托福
- 大庆代考托福