117. 填充每个节点的下一个右侧节点指针 II
小于 1 分钟
117. 填充每个节点的下一个右侧节点指针 II中等
解法:
层序遍历
class Solution {
public Node connect(Node root) {
if(root == null) return null;
List<Node> list = new ArrayList<Node>();
list.add(root);
setNext(list);
return root;
}
public void setNext(List<Node> list) {
if(list.size() == 0) return;
List<Node> arr = new ArrayList<>();
for(int i = 0;i < list.size();i++) {
Node node = list.get(i);
if(i + 1 < list.size()) {
node.next = list.get(i + 1);
}
if(node.left != null) {
arr.add(node.left);
}
if(node.right != null) {
arr.add(node.right);
}
}
setNext(arr);
}
}
Powered by Waline v2.15.5