[LeetCode] 117. Populating Next Right Pointers in Each Node II 每个节点的右向指针 II
Follow up for problem "Populating Next Right Pointers in Each Node".
What if the given tree could be any binary tree? Would your previous solution still work?
Note:
- You may only use constant extra space.
For example,
Given the following binary tree,
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
116. Populating Next Right Pointers in Each Node 的延续,如果给定的树是任何二叉树,不一定是完全二叉树,就是说不是每个节点都包含有两个子节点。
解法1:BFS, easy but not constant space, Complexity: time O(N) space O(N) - queue
解法2: Iteration - use dummy node to keep record of the next level's root to refer pre travel current level by referring to root in the level above,Complexity: time O(N) space O(1)
Java:BFS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class Solution { public void connect(TreeLinkNode root) { if (root == null ) return ; Queue<TreeLinkNode> nodes = new LinkedList<>(); nodes.offer(root); while (!nodes.isEmpty()){ int size = nodes.size(); for ( int i = 0 ; i < size; i++){ TreeLinkNode cur = nodes.poll(); TreeLinkNode n = null ; if (i < size - 1 ){ n = nodes.peek(); } cur.next = n; if (cur.left != null )nodes.offer(cur.left); if (cur.right != null )nodes.offer(cur.right); } } } } |
Java: Iteration
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public void connect(TreeLinkNode root) { TreeLinkNode dummy = new TreeLinkNode( 0 ); TreeLinkNode pre = dummy; //record next root while (root != null ){ if (root.left != null ){ pre.next = root.left; pre = pre.next; } if (root.right != null ){ pre.next = root.right; pre = pre.next; } root = root.next; //reach end, update new root & reset dummy if (root == null ){ root = dummy.next; pre = dummy; dummy.next = null ; } } } } |
Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | public void connect(TreeLinkNode root) { if (root == null ) return ; TreeLinkNode lastHead = root; //prevous level's head TreeLinkNode lastCurrent = null ; //previous level's pointer TreeLinkNode currentHead = null ; //currnet level's head TreeLinkNode current = null ; //current level's pointer while (lastHead!= null ){ lastCurrent = lastHead; while (lastCurrent!= null ){ //left child is not null if (lastCurrent.left!= null ) { if (currentHead == null ){ currentHead = lastCurrent.left; current = lastCurrent.left; } else { current.next = lastCurrent.left; current = current.next; } } //right child is not null if (lastCurrent.right!= null ){ if (currentHead == null ){ currentHead = lastCurrent.right; current = lastCurrent.right; } else { current.next = lastCurrent.right; current = current.next; } } lastCurrent = lastCurrent.next; } //update last head lastHead = currentHead; currentHead = null ; } } |
Python:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class TreeNode: def __init__( self , x): self .val = x self .left = None self .right = None self . next = None def __repr__( self ): if self is None : return "Nil" else : return "{} -> {}" . format ( self .val, repr ( self . next )) class Solution: # @param root, a tree node # @return nothing def connect( self , root): head = root while head: prev, cur, next_head = None , head, None while cur: if next_head is None : if cur.left: next_head = cur.left elif cur.right: next_head = cur.right if cur.left: if prev: prev. next = cur.left prev = cur.left if cur.right: if prev: prev. next = cur.right prev = cur.right cur = cur. next head = next_head |
C++:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Solution { public : void connect(TreeLinkNode *root) { TreeLinkNode *dummy = new TreeLinkNode(0), *t = dummy; while (root) { if (root->left) { t->next = root->left; t = t->next; } if (root->right) { t->next = root->right; t = t->next; } root = root->next; if (!root) { t = dummy; root = dummy->next; dummy->next = NULL; } } } }; |
类似题目:
[LeetCode] 116. Populating Next Right Pointers in Each Node 每个节点的右向指针
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步