今際の国の呵呵君
Wednesday, October 11, 2017
[LeetCode]Populating Next Right Pointers in Each Node
题目说了限定perfect binary tree,那么很明显递归就可以做。我们在当前节点把子节点的next pointer连好,然后递归子节点即可。假设树有n节点,时间复杂度O(n),空间复杂度O(log n),代码如下:
当然我们还可以层序遍历,每一层从左到右把子节点连好即可。时间复杂度O(n),常数空间,代码如下:
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment