struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
} 填充所有节点的next指针,指向最接近它的同一层右边节点。如果没有同一层没有右边的节点,则应该将next指针设置为NULL。- 你只能使用常量级的额外内存空间
- 可以假设给出的二叉树是一个完美的二叉树(即,所有叶子节点都位于同一层,而且每个父节点都有两个孩子节点)。
struct TreeLinkNode {
TreeLinkNode *left;
TreeLinkNode *right;
TreeLinkNode *next;
} 填充所有节点的next指针,指向最接近它的同一层右边节点。如果没有同一层没有右边的节点,则应该将next指针设置为NULL。
import java.util.*;
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) {
} else {
TreeLinkNode node = root;
Stack<TreeLinkNode>list = new Stack<TreeLinkNode>();
list.push(node);
while (list.get(0).left!=null) {
int num = list.size();
for (int i = 0; i < num; i++) {
if (i <num - 1) {
list.get(0).next = list.get(0);
} else {
list.get(0).next = null;
}
list.push(list.get(0).left);
list.push(list.get(0).right);
list.remove(0);
}
}
for(int i=0;i<list.size();i++){
if (i <list.size() - 1) {
list.get(i).next = list.get(i);
} else {
list.get(i).next = null;
}
}
}
}
} 请问各位大佬为什么这段代码能提交成功,明明是我手误写错了,却通过了全部用例
Java6行搞定
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null || root.left == null) return;
root.left.next = root.right;
if (root.next != null)
root.right.next = root.next.left;
connect(root.left);
connect(root.right);
}
}
import java.util.*;
public class Solution {
Queue<TreeLinkNode> queue = new LinkedList<>();
TreeLinkNode node,preNode =null;
int count=1;
int n=1;
public void connect(TreeLinkNode root) {
if(root==null) return;
queue.offer(root);
while(!queue.isEmpty()){
node=queue.poll();
if(count==n){//2的幂次方,则上一个指向null
if(preNode!=null){
preNode.next=null;
}
n=n*2;
}
else{
preNode.next=node;//横向指针
}
if(node.left!=null){
queue.offer(node.left);
queue.offer(node.right);
}
preNode=node;
count++;
}
}
} import java.util.*;
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
Queue<TreeLinkNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
int n = queue.size();
for (int i = 0; i < n; ++i) {
TreeLinkNode node = queue.poll();
if (i < n - 1) {
node.next = queue.peek();
}
if (node.left != null)
queue.offer(node.left);
if (node.right != null)
queue.offer(node.right);
}
}
}
} import java.util.*;
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) return;
TreeLinkNode leftmost = root;
while (leftmost.left != null) {
//leftmost是每一层的第一个节点
TreeLinkNode head = leftmost;
//在同一层中从左向右遍历
while (head != null) {
//首先在该结点的左孩子和右孩子连接起来
head.left.next = head.right;
//再把该节点的右孩子同该节点的右测节点的左孩子连接起来
if (head.next != null) {
head.right.next = head.next.left;
}
//从左向右遍历
head = head.next;
}
//当前层遍历完成后转移到下一层
leftmost = leftmost.left;
}
}
} package LeetCode;
import java.util.*;
class TreeLinkNode {
int val;
TreeLinkNode left, right, next;
TreeLinkNode(int x) { val = x; }
}
public class Solution {
public void connect(TreeLinkNode root) {
if(root==null)
return ;
Queue<TreeLinkNode> queue = new LinkedList<>();
queue.add(root);
int size= queue.size();
while(!queue.isEmpty()) {
TreeLinkNode node = queue.remove();
size--;
if(node.left!=null) {
queue.add(node.left);
}
if(node.right!=null) {
queue.add(node.right);
}
if(size==0) {
node.next=null;
size=queue.size();
}else {
node.next=queue.peek();
}
}
}
}
这道题可以使用层次遍历的思想解决。
import java.util.*;
public class Solution {
public void connect(TreeLinkNode root) {
Queue<TreeLinkNode> queue = new LinkedList<TreeLinkNode>();
if(root != null){
root.next = null;
queue.add(root);
}
TreeLinkNode node = null;
while(queue.size() > 0){
int size = queue.size();
for(int i = 0; i < size; i++){
node = queue.poll();
if(i == size - 1){
node.next = null;
}else{
node.next = queue.peek();
}
if(node.left != null){
queue.add(node.left);
}
if(node.right != null){
queue.add(node.right);
}
}
}
}
} public class Solution {
public void connect(TreeLinkNode root) {
if(root==null)
return;
TreeLinkNode p=root;
TreeLinkNode q=new TreeLinkNode(0);
while(p.left!=null){
q=p;
while(q!=null){
q.left.next=q.right;
if(q.next!=null)
q.right.next=q.next.left;
q=q.next;
}
p=p.left;
}
return;
}
} /**
* Definition for binary tree with next pointer.
* public class TreeLinkNode {
* int val;
* TreeLinkNode left, right, next;
* TreeLinkNode(int x) { val = x; }
* }
*/
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public void connect(TreeLinkNode root) {
if(root == null)
return ;
//思路:注意到二叉树的每一层只有最后一个节点的next指向null。
//因此联想到二叉树的层次遍历,用队列保存每一层的节点,每层除了最后一个节点
//其他的节点的next均指向下一个节点
Queue<TreeLinkNode> queue = new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty())
{
int length = queue.size();
for(int i = 0; i < length; i++)
{
TreeLinkNode linkNode = queue.poll();
if(i == length -1)
linkNode.next = null;
else
{
linkNode.next = queue.peek();
}
if(linkNode.left != null)
queue.offer(linkNode.left);
if(linkNode.right != null)
queue.offer(linkNode.right);
}
}
}
} //层次遍历
public void connect(TreeLinkNode root) {
if (root==null)
return;
LinkedList<TreeLinkNode> queue=new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()){
int size=queue.size();
for (int i=0;i<size;i++){
TreeLinkNode node= queue.removeFirst();
if (i+1<size)
node.next= queue.get(0);;
if (node.left!=null)
queue.add(node.left);
if (node.right!=null)
queue.add(node.right);
}
}
}
public static class TreeLinkNode{
int val;
TreeLinkNode left;
TreeLinkNode right;
TreeLinkNode next;
public TreeLinkNode(int val) {
// TODO Auto-generated constructor stub
this.val = val;
}
}
public static class TreeLinkNodeOrder{
TreeLinkNode node;
int order;
public TreeLinkNodeOrder(TreeLinkNode node, int order) {
// TODO Auto-generated constructor stub
this.node = node;
this.order = order;
}
}
public static void connect(TreeLinkNode root) {
if (root == null) {
return;
}
Queue<TreeLinkNodeOrder> queue = new LinkedList<>();
queue.add(new TreeLinkNodeOrder(root, 1));
while (!queue.isEmpty()) {
TreeLinkNodeOrder tmp = queue.poll();
TreeLinkNode node = tmp.node;
int order = tmp.order;
if (!queue.isEmpty() && order == queue.peek().order) {
node.next = queue.peek().node;
} else {
node.next = null;
}
if (node.left != null) {
queue.add(new TreeLinkNodeOrder(node.left, order+1));
}
if (node.right != null) {
queue.add(new TreeLinkNodeOrder(node.right, order+1));
}
}
}
public class Solution {
public void connect(TreeLinkNode root) {
if(root==null)return;
while(root.left!=null){
TreeLinkNode node=root;
while(node!=null){
node.left.next=node.right;
if(node.next!=null)node.right.next=node.next.left;
node=node.next;
}
root=root.left;
}
}
} //按行打印二叉树+一个栈
public static void connect(TreeLinkNode root) {
LinkedList<TreeLinkNode> linkedList = new LinkedList();
Stack<TreeLinkNode> s = new Stack<>();
if (root == null) {
return;
}
linkedList.add(root);
TreeLinkNode next = null;
TreeLinkNode cur = null;
int start = 0;
int end = 1;
while (!linkedList.isEmpty()) {
TreeLinkNode temp = linkedList.pollFirst();
s.push(temp);
start++;
if (temp.left != null) {
linkedList.add(temp.left);
}
if (temp.right != null) {
linkedList.add(temp.right);
}
if (start == end){
while (!s.isEmpty()){
cur = s.pop();
cur.next = next;
next = cur;
}
next = null;
start = 0;
end = linkedList.size();
}
}
}
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null)
return;
if (root.left == null && root.right == null)
return;
else if (root.left == null)
connect(root.right);
else if (root.right == null)
connect(root.left);
else {
root.left.next = root.right;
}
if (root.next != null && root.right != null) {
root.right.next = root.next.left;
}
connect(root.left);
connect(root.right);
}
}
public class Solution {
public void connect(TreeLinkNode root) {
if (root == null) {
return;
}
TreeLinkNode cur = null;
TreeLinkNode pre = null;
TreeLinkNode nextLineFirst = null;
cur = root;
while (cur != null) {
while(cur != null) {
if (cur.left != null) {
if (nextLineFirst == null) {
nextLineFirst = cur.left;
}
if (pre != null) {
pre.next = cur.left;
}
pre = cur.left;
}
if (cur.right != null) {
if (nextLineFirst == null) {
nextLineFirst = cur.right;
}
if (pre != null) {
pre.next = cur.right;
}
pre = cur.right;
}
cur = cur.next;
}
pre = null;
cur = nextLineFirst;
nextLineFirst = null;
}
}
} //层次遍历,从右至左
public void connect(TreeLinkNode root) {
if(root==null) return;
LinkedList que=new LinkedList();
que.offer(root);
while(!que.isEmpty()){
int size=que.size();
TreeLinkNode pre =null;
while (size>0){
size--;
TreeLinkNode p = que.poll();
p.next=pre;
pre=p;
if(p.right!=null) que.offer(p.right);
if(p.left!=null) que.offer(p.left);
}
}
}