完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。
116. 填充每个节点的下一个右侧节点指针
第一反应是bfs,不推荐
递归
var connect = function(root) {
if (root == null) return null;
connectNodes(root.left, root.right);
return root;
};
function connectNodes (node1, node2) {
if (node1 == null || node2 == null) return;
node1.next = node2;
connectNodes(node1.left, node1.right);
connectNodes(node2.left, node2.right);
connectNodes(node1.right, node2.left);
}