# Middle-题目17：116. Populating Next Right Pointers in Each Node

55人阅读 评论(0)

Given a binary tree

struct TreeLinkNode {
}


Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
For example,
Given the following perfect binary tree,

         1
/  \
2    3
/ \  / \
4  5  6  7

After calling your function, the tree should look like:

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

public class Solution {
if(root==null)
return;
else if(root.left == null && root.right == null && root.next == null) { // root is a leaf node
root.next=null;
return;
}
else {
if(root.left != null)
root.left.next = root.right;
if(root.right != null && root.next != null)
root.right.next = root.next.left;
if(root.right != null && root.next == null)
root.right.next = null;
connect(root.left);
connect(root.right);
}
}
}

0ms，beats 81.60%，众数1ms，52.14%

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：56042次
• 积分：3202
• 等级：
• 排名：第10679名
• 原创：270篇
• 转载：53篇
• 译文：0篇
• 评论：7条
文章分类
最新评论