Populating Next Right Pointers in Each Node II
第10天了。
今天的题目是 117. Populating Next Right Pointers in Each Node II :
Given a binary tree
1 | struct Node { |
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
.
Initially, all next pointers are set to NULL
.
Example:
1 | Input: {"$id":"1","left":{"$id":"2","left":{"$id":"3","left":null,"next":null,"right":null,"val":4},"next":null,"right":{"$id":"4","left":null,"next":null,"right":null,"val":5},"val":2},"next":null,"right":{"$id":"5","left":null,"next":null,"right":{"$id":"6","left":null,"next":null,"right":null,"val":7},"val":3},"val":1} |
Note:
- You may only use constant extra space.
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
这是一道之前没做出来的问题,最开始想出来的解法也和之前差不多,大概的想法是递归求解时返回子树的最左和最右节点,然后通过一些判断来相连,但是这个问题主要是没法处理两个子树高度不一样的问题。
后面尝试用分治的方法来做,主要的想法是,假设我现在已经有了连接好的左子树和右子树,现在只需要将两个子树连接起来即可。而连接方法就是一层一层的去连接两个子树:
1 | Node *nextLayer(Node *root) { |
这个方法的时间复杂度大概是O(h^2)
,其中h
是树的高度。
后面又发现一种方法,这种方法大概的思路是连接孩子,然后在递归求解。这样会保证在求解到root
节点时,root
节点的next
是已知的,同时在连接孩子时,需要利用到右子树的next
指针,所以需要先求解右子树再求解左子树。
1 | void connectChild(Node *root) { |