- // 按层从左往右检索
- public class Solution {
- public void connect(TreeLinkNode root) {
- while (root != null) {
- TreeLinkNode tempChild = new TreeLinkNode(0);
- TreeLinkNode currentChild = tempChild;
- while (root != null) {
- if (root.left != null) {
- currentChild.next = root.left;
- currentChild = currentChild.next;
- }
- if (root.right != null) {
- currentChild.next = root.right;
- currentChild = currentChild.next;
- }
- root = root.next;
- }
- root = tempChild.next;
- }
- }
- }
来源: http://www.bubuko.com/infodetail-2988076.html