//蚂蚁一面 评测题目: 给定一个 完美二叉树 ,其所有叶子节点都在同一层,每个父节点都有两个子节点,
//填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。
//初始状态下,所有 next 指针都被设置为 NULL。
public class Test {
public static void main(String[] args) {
// pointToNext(root);
}
public static void pointToNext(TreeNode root) {
if(root == null) return;
// 之字遍历用队列
List<TreeNode> queue = new ArrayList<>();
queue.add(root);
while(queue.size() != 0){
int i=0;
// 遍历的同时,搞定next指针
while(i < queue.size()-1){
queue.get(i).next = queue.get(i+1);
i++;
}
for(int j=0; j <= i; j++){
TreeNode head = queue.get(0);
queue.add(head.left);
queue.add(head.right);
head.remove(0);
}
}
}
}