LeetCode中级算法-每个节点的右向指针

18 篇文章 0 订阅
4 篇文章 0 订阅

给定一个二叉树

struct TreeLinkNode {
  TreeLinkNode *left;
  TreeLinkNode *right;
  TreeLinkNode *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

说明:

  • 你只能使用额外常数空间。
  • 使用递归解题也符合要求,本题中递归程序占用的栈空间不算做额外的空间复杂度。
  • 你可以假设它是一个完美二叉树(即所有叶子节点都在同一层,每个父节点都有两个子节点)。

示例:

给定完美二叉树,

     1
   /  \
  2    3
 / \  / \
4  5  6  7

调用你的函数后,该完美二叉树变为:

     1 -> NULL
   /  \
  2 -> 3 -> NULL
 / \  / \
4->5->6->7 -> NULL

 

 

题目还是很简单的,因为限定了是完美二叉树

但是第一遍代码写得太冗余了,完全是靠人工强行判断,虽然通过了平台的检测,但是看着难受,想办法优化一下

import java.util.LinkedList;
import java.util.Queue;

public class RightNode {
    public void connect(TreeLinkNode root){

        if(root!=null){
            Queue<TreeLinkNode> firstQueue=new LinkedList<>();
            Queue<TreeLinkNode> secondQueue=new LinkedList<>();

            firstQueue.add(root);

            boolean flag=true;

            while (true){
                TreeLinkNode temp1;
                TreeLinkNode temp2;

                if(flag) {

                    if (firstQueue.size() == 1) {
                        TreeLinkNode temp=firstQueue.poll();
                        temp.next=null;
                        if(temp.right!=null) {
                            secondQueue.add(temp.left);
                            secondQueue.add(temp.right);
                        }
                        flag=false;
                    } else if(firstQueue.size()!=0){
                        temp1 = firstQueue.poll();
                        if(temp1.right!=null) {
                            secondQueue.add(temp1.left);
                            secondQueue.add(temp1.right);
                        }
                            temp2 = firstQueue.poll();
                        if(temp2.right!=null) {
                            secondQueue.add(temp2.left);
                            secondQueue.add(temp2.right);
                        }
                            temp1.next = temp2;


                        while (!firstQueue.isEmpty()) {
                            temp1 = temp2;
                            temp2 = firstQueue.poll();
                            if(temp2.right!=null) {
                                secondQueue.add(temp2.left);
                                secondQueue.add(temp2.right);
                            }
                            temp1.next = temp2;
                        }

                        temp2.next = null;
                        flag=false;
                    }else if(firstQueue.size()==0){
                        break;
                    }

                }else{
                    if(secondQueue.size()!=0){
                        temp1 = secondQueue.poll();
                        if(temp1.right!=null) {
                            firstQueue.add(temp1.left);
                            firstQueue.add(temp1.right);
                        }
                        temp2 = secondQueue.poll();
                        if(temp2.right!=null) {
                            firstQueue.add(temp2.left);
                            firstQueue.add(temp2.right);
                        }

                        temp1.next = temp2;
                        while (!secondQueue.isEmpty()) {
                            temp1 = temp2;
                            temp2 = secondQueue.poll();
                            if(temp2.right!=null) {
                                firstQueue.add(temp2.left);
                                firstQueue.add(temp2.right);
                            }
                            temp1.next = temp2;
                        }

                        temp2.next = null;
                        flag=true;
                    }else  if(secondQueue.size()==0){
                        break;
                    }
                }

            }
        }


    }
}






 class TreeLinkNode {
      int val;
      TreeLinkNode left, right, next;
      TreeLinkNode(int x) { val = x; }
  }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值