[Java]LeetCode117. 填充同一层的兄弟节点 II | Populating Next Right Pointers in Each Node II
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:https://www.cnblogs.com/strengthen/p/9953121.html
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
Given a binary tree
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
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
.
Note:
- You may only use constant extra space.
- Recursive approach is fine, implicit stack space does not count as extra space for this problem.
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
给定一个二叉树
struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL
。
初始状态下,所有 next 指针都被设置为 NULL
。
说明:
- 你只能使用额外常数空间。
- 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
示例:
给定二叉树,
1 / \ 2 3 / \ \ 4 5 7
调用你的函数后,该二叉树变为:
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
0ms
1 /* 2 // Definition for a Node. 3 class Node { 4 public int val; 5 public Node left; 6 public Node right; 7 public Node next; 8 9 public Node() {} 10 11 public Node(int _val,Node _left,Node _right,Node _next) { 12 val = _val; 13 left = _left; 14 right = _right; 15 next = _next; 16 } 17 }; 18 */ 19 class Solution { 20 public Node connect(Node root) { 21 Node node = root; 22 while(node != null){ 23 Node tempChild = new Node(0); 24 Node currentChild = tempChild; 25 while(node!=null){ 26 if(node.left != null) { currentChild.next = node.left; currentChild = currentChild.next;} 27 if(node.right != null) { currentChild.next = node.right; currentChild = currentChild.next;} 28 node = node.next; 29 } 30 node = tempChild.next; 31 } 32 return root; 33 } 34 }
1ms
1 class Solution { 2 public Node connect(Node root) { 3 connect(root, null); 4 return root; 5 } 6 7 private void connect(Node root, Node nxtRight) { 8 if(root == null) { 9 return; 10 } 11 12 root.next = nxtRight; 13 Node nxtRightForRight = null; 14 Node curr = root.next; 15 while(curr != null) { 16 if(curr.left != null) { 17 nxtRightForRight = curr.left; 18 break; 19 } else if(curr.right != null) { 20 nxtRightForRight = curr.right; 21 break; 22 } 23 curr = curr.next; 24 } 25 connect(root.right, nxtRightForRight); 26 connect(root.left, root.right == null ? nxtRightForRight : root.right); 27 return; 28 } 29 }
2ms
1 class Solution { 2 public Node connect(Node root) { 3 if (root == null) { 4 return root; 5 } 6 return connectHelper(root,0,new HashMap<Integer,Node>());//map put the last node of every level 7 } 8 9 private Node connectHelper(Node node,int level,Map<Integer,Node> map) { 10 11 if (node == null) { 12 return node; 13 } 14 Node last = null; 15 if (map.containsKey(level)) { 16 last = map.get(level); 17 last.next = node; 18 } 19 map.put(level,node); 20 Node left = connectHelper(node.left,level+1,map); 21 Node right = connectHelper(node.right,level+1,map); 22 23 return node; 24 } 25 }
3ms
1 class Solution { 2 public Node connect(Node root) { 3 if (root == null) { 4 return root; 5 } 6 Deque<Node> queue = new LinkedList<>(); 7 queue.offer(root); 8 while (!queue.isEmpty()) { 9 int size = queue.size(); 10 Node prev = null,cur = null; 11 for (int i = 0; i < size; i++) { 12 cur = queue.poll(); 13 if (prev != null) { 14 prev.next = cur; 15 } 16 prev = cur; 17 18 if (cur.left != null) { 19 queue.offer(cur.left); 20 } 21 if (cur.right != null) { 22 queue.offer(cur.right); 23 } 24 } 25 cur.next = null; 26 } 27 return root; 28 } 29 }
4ms
1 class Solution { 2 public Node connect(Node root) { 3 if(root==null){ 4 return null; 5 } 6 LinkedList<Node> stack=new LinkedList<>(); 7 LinkedList<Node> temp=new LinkedList<>(); 8 List<List<Node>> list=new ArrayList<List<Node>>(); 9 List<Node> l=new ArrayList<>(); 10 stack.addFirst(root); 11 while(!stack.isEmpty()){ 12 Node rp=stack.removeFirst(); 13 l.add(rp); 14 if(rp.left!=null){ 15 temp.addLast(rp.left); 16 } 17 if(rp.right!=null){ 18 temp.addLast(rp.right); 19 } 20 if(stack.isEmpty()){ 21 stack=temp; 22 temp=new LinkedList<>(); 23 list.add(l); 24 l=new ArrayList<>(); 25 } 26 } 27 for(int i=0;i<list.size();i++){ 28 for(int j=0;j<list.get(i).size();j++){ 29 if(j==list.get(i).size()-1){ 30 Node s=list.get(i).get(j); 31 s.next=null; 32 break; 33 } 34 Node a=list.get(i).get(j); 35 a.next=list.get(i).get(j+1); 36 } 37 } 38 return root; 39 } 40 }
5ms
1 class Solution { 2 public Node connect(Node root) { 3 if (root == null) { 4 return null; 5 } 6 7 Node lastHead = root; 8 Node curHead = lastHead.left != null ? lastHead.left : lastHead.right; 9 10 Node last = lastHead; 11 Node cur = curHead; 12 13 while (cur != null) { 14 Node nxt = null; 15 while (last != null && nxt == null) { 16 if (cur == last.left) { 17 if (last.right != null) { 18 nxt = last.right; 19 } else { 20 last = last.next; 21 } 22 } else if (cur == last.right) { 23 last = last.next; 24 } else { 25 if (last.left != null) { 26 nxt = last.left; 27 } else if (last.right != null) { 28 nxt = last.right; 29 } else { 30 last = last.next; 31 } 32 } 33 } 34 35 cur.next = nxt; 36 cur = cur.next; 37 38 // System.out.println(cur != null ? cur.val + " " : "null "); 39 if (cur == null) { 40 lastHead = curHead; 41 while (lastHead != null && lastHead.left == null && lastHead.right == null) { 42 lastHead = lastHead.next; 43 } 44 45 // System.out.println(" " + lastHead.left + " " + lastHead.right + " " + lastHead.next.val); 46 47 if (lastHead == null) { 48 break; 49 } 50 curHead = lastHead.left != null ? lastHead.left : lastHead.right; 51 52 last = lastHead; 53 cur = curHead; 54 if (cur != null) { 55 System.out.println(lastHead.val + " " + curHead.val); 56 } 57 } 58 } 59 return root; 60 } 61 }