★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-rtptiagb-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
Given a binary treenode
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
.git
Initially, all next pointers are set to NULL
.github
Note:微信
Example:app
Given the following binary tree,函數
1 / \ 2 3 / \ \ 4 5 7
After calling your function, the tree should look like:this
1 -> NULL / \ 2 -> 3 -> NULL / \ \ 4-> 5 -> 7 -> NULL
給定一個二叉樹spa
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 }