题目:
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.
- You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).
For example,
Given the following perfect binary tree,
1 / 2 3 / / 4 5 6 7
After calling your function, the tree should look like:
1 -> NULL / 2 -> 3 -> NULL / / 4->5->6->7 -> NULL
思路1:使用深度优先搜索遍历
代码:
public static void connect_DFS(TreeLinkNode root) { if(root != null){ if(root.left != null && root.right != null){ root.left.next = root.right; System.out.println(root.left.val +" Next : "+ root.right.val); if(root.left.right != null && root.right.left != null){ root.left.right.next = root.right.left; System.out.println(root.left.right.val +" Next : "+ root.right.left.val); } } if(root.left != null ){ connect_DFS(root.left); } if(root.right != null ){ connect_DFS(root.right); } } }
分析:存在缺点:第一左子树的最右边的节点的情况处理不到,分析貌似用深搜不行,后来看了网上高人的解答,发现:root.right.next = root.next.left
更改代码为:
public class Solution { public static void connect(TreeLinkNode root) { if(root != null){ if(root.left != null && root.right != null){ root.left.next = root.right; //System.out.println(root.left.val +" Next : "+ root.right.val); if(root.next!= null){ root.right.next = root.next.left; //System.out.println(root.right.val +" Next : "+ root.next.left.val); } } if(root.left != null ){ connect(root.left); } if(root.right != null ){ connect(root.right); } } } }
思路二:采用广度优先搜索
代码:
/** * Definition for binary tree with next pointer. * public class TreeLinkNode { * int val; * TreeLinkNode left, right, next; * TreeLinkNode(int x) { val = x; } * } */ public class Solution { public static void connect(TreeLinkNode root) { if(root!= null){ Queue<TreeLinkNode> queListFlag = new LinkedList<TreeLinkNode>(); queListFlag.add(root); while(queListFlag.size()!=0){ ArrayList arrayTemp=new ArrayList(); while(queListFlag.size()!=0){ arrayTemp.add(queListFlag.peek()); queListFlag.remove(); } if(arrayTemp.size()==1){ TreeLinkNode NodeTemp = (TreeLinkNode)arrayTemp.get(0); if(NodeTemp.left != null && NodeTemp.right != null){ NodeTemp.left.next = NodeTemp.right; //System.out.println(NodeTemp.left.val +" Next : "+ NodeTemp.right.val); } if(NodeTemp.left != null ){ queListFlag.add(root.left); } if(NodeTemp.right != null ){ queListFlag.add(root.right); } }else if(arrayTemp.size()>1){ for(int i=0;i<arrayTemp.size()-1;i++) { TreeLinkNode NodeTemp1 = (TreeLinkNode)arrayTemp.get(i); TreeLinkNode NodeTemp2 = (TreeLinkNode)arrayTemp.get(i+1); if(NodeTemp1.left != null && NodeTemp1.right != null){ NodeTemp1.left.next = NodeTemp1.right; //System.out.println(NodeTemp1.left.val +" Next : "+ NodeTemp1.right.val); } if(NodeTemp1.right != null && NodeTemp2.left != null){ NodeTemp1.right.next = NodeTemp2.left; //System.out.println(NodeTemp1.right.val +" Next : "+ NodeTemp2.left.val); } if(i == arrayTemp.size()-2){ if(NodeTemp2.left != null && NodeTemp2.right != null){ NodeTemp2.left.next = NodeTemp2.right; //System.out.println(NodeTemp2.left.val +" Next : "+ NodeTemp2.right.val); } } } for(int i=0;i<arrayTemp.size();i++) { TreeLinkNode NodeTemp = (TreeLinkNode)arrayTemp.get(i); if(NodeTemp.left != null ){ queListFlag.add(NodeTemp.left); } if(NodeTemp.right != null ){ queListFlag.add(NodeTemp.right); } } } } } } }
思路三:不采用其它的存储空间
题目要求:You may only use constant extra space.
所以逐层遍历节点的时候,需要将下一层节点的都连接上
抄袭高人的代码如下:
public class Solution { public void connect(TreeLinkNode root) { TreeLinkNode levelHead = root, nextLevelHead = null; while (levelHead != null) { TreeLinkNode node = levelHead, tail = null; while (node != null) { if (node.left != null && node.right != null) { node.left.next = node.right; } TreeLinkNode sub; if (node.left != null) sub = node.left; else if (node.right != null) sub = node.right; else sub = null; if (sub != null) { if (nextLevelHead == null) { nextLevelHead = sub; tail = sub; } else { tail.next = sub; } while (tail.next != null) tail = tail.next; } node = node.next; } levelHead = nextLevelHead; nextLevelHead = null; } } }