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
https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
思路1: 时间O(N), 空间O(1)。一层一层处理,维护一些变量。
思路2: 层序遍历,时空O(N)。
思路1:
class Solution { Node prev, leftmost; public void processChild(Node childNode) { if (childNode != null) { // If the "prev" pointer is alread set i.e. if we // already found atleast one node on the next level, // setup its next pointer if (this.prev != null) { this.prev.next = childNode; } else { // Else it means this child node is the first node // we have encountered on the next level, so, we // set the leftmost pointer this.leftmost = childNode; } this.prev = childNode; } } public Node connect(Node root) { if (root == null) { return root; } // The root node is the only node on the first level // and hence its the leftmost node for that level this.leftmost = root; // Variable to keep track of leading node on the "current" level Node curr = leftmost; // We have no idea about the structure of the tree, // so, we keep going until we do find the last level. // the nodes on the last level won't have any children while (this.leftmost != null) { // "prev" tracks the latest node on the "next" level // while "curr" tracks the latest node on the current // level. this.prev = null; curr = this.leftmost; // We reset this so that we can re-assign it to the leftmost // node of the next level. Also, if there isn't one, this // would help break us out of the outermost loop. this.leftmost = null; // Iterate on the nodes in the current level using // the next pointers already established. while (curr != null) { // Process both the children and update the prev // and leftmost pointers as necessary. this.processChild(curr.left); this.processChild(curr.right); // Move onto the next node. curr = curr.next; } } return root ; } }
第二遍复习:经人点拨此题用bfs层序遍历来做很简洁,但是有queue空间就不是O(1)了
public class Solution { public void connect(TreeLinkNode root) { if (root == null) return; Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>(); queue.add(root); queue.add(null); while (!queue.isEmpty()) { TreeLinkNode out = queue.remove(); if (out != null) { out.next = queue.peek(); if (out.left != null) queue.add(out.left); if (out.right != null) queue.add(out.right); } else { if (!queue.isEmpty()) { queue.add(null); } } } } }
某大神的解法
public void connect(TreeLinkNode root) { TreeLinkNode dummyHead = new TreeLinkNode(0); TreeLinkNode pre = dummyHead; 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; if (root == null) { pre = dummyHead; root = dummyHead.next; dummyHead.next = null; } } }