populating-next-right-pointers-in-each-node II

I Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.

Initially, all next pointers are set toNULL.

简单递归
/**
 * Created by a819 on 2017/8/8.
 */
class TreeLinkNode {
    int val;
    TreeLinkNode left, right, next;
    TreeLinkNode(int x) { val = x; }
}
public class Connect {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return;
        else {
            con(root);
            // root.next=null;
        }



    }

    public void con(TreeLinkNode root){
        if (root.left != null && root.right != null) {
            root.left.next = root.right;
            if (root.next!=null)
                root.right.next=root.next.left;
            connect(root.left);
            connect(root.right);

        }
        else return;
    }
}

II 循环遍历(可能不是完全二叉树)

/**
 * 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 void connect(TreeLinkNode root) {
       if(root==null)
            return;
        TreeLinkNode lastnode=root;//每一行的头结点
        TreeLinkNode curhead=null;//定义当前行的头结点
        TreeLinkNode prenode=null;//定义当前行向后遍历的节点
        while(lastnode!=null)
        {
            TreeLinkNode nextnode=lastnode; //定义当前行的遍历节点
            while(nextnode!=null)
            {
                if(nextnode.left!=null)  //说明有左孩子节点
                {
                    //判断当前有没有头结点,即该节点是不是下一行第一个节点
                    if(curhead==null) //没有头结点
                    {
                        curhead=nextnode.left;
                        prenode=curhead;
                    }else{ //有头结点,prenode向后前进
                        prenode.next=nextnode.left;
                        prenode=prenode.next;
                    }
                }

                if(nextnode.right!=null) //说明有右孩子节点
                {
                    //同样需要判断当前有没有头结点
                    if(curhead==null) //没有头结点
                    {
                        curhead=nextnode.right;
                        prenode=curhead;
                    }else{ //有头结点,prenode向后前进
                        prenode.next=nextnode.right;
                        prenode=prenode.next;
                    }
                }
                nextnode=nextnode.next;
            }
            //当前行结束,进入下一行
            lastnode=curhead;
            curhead=null;   //需要置为null,因为是新的一层需要建立
        } 
    }
}
II 思想转自http://www.programcreek.com/2014/05/leetcode-populating-next-right-pointers-in-each-node-java/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值