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

描述:

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 toNULL.

Initially, all next pointers are set toNULL.

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
思路:

采用层次遍历的思想,采用队列,如果需要确定层次,需要另外的辅助指针。
Now:当前遍历的节点
Last:这层最后的节点
怎么能找到每一层的最后节点呢?需要nLast指针,每次更新为新加入队列的指针。想一下,当Now指针指向Last时,nLast就更新为了下一层最后一个节点。

C++代码:
/**
 * Definition for binary tree with next pointer.
 * struct TreeLinkNode {
 *  int val;
 *  TreeLinkNode *left, *right, *next;
 *  TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
 * };
 */
class Solution {
public:
    void connect(TreeLinkNode *root) {
        
        //如果树为空
        if(root == NULL)
            return;
        //定义辅助指针
        TreeLinkNode* last = root;
        TreeLinkNode* nLast = NULL;
        TreeLinkNode* now = root;
        
        queue<TreeLinkNode*> qu;
        vector<TreeLinkNode*> ve;
        qu.push(now);
        
        while(qu.size() != 0)
        {
            now = qu.front();
            ve.push_back(now);
            qu.pop();
            if(now->left != NULL)
            {
                qu.push(now->left);
                nLast = now->left;
            }
            if(now->right != NULL)
            {
                qu.push(now->right);
                nLast = now->right;
            }
            
            //遍历到最后节点,按照层顺序连接起来
            if(now == last)
            {
                for(int i = 0;i<ve.size()-1;i++)
                {
                    ve[i]->next = ve[i+1];
                }
                ve.clear();
                last = nLast;
            }
        }
    }
};
Java代码:
/**
 * 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;
import java.util.ArrayList;
public class Solution {
    public void connect(TreeLinkNode root) {
        if(root == null)
            return;
        
        TreeLinkNode now = root;
        TreeLinkNode last = root;
        TreeLinkNode nlast = null;
        
        Queue<TreeLinkNode> qu = new LinkedList<TreeLinkNode>();
        ArrayList<TreeLinkNode> list = new ArrayList<>();
        qu.add(now);
        
        while(qu.peek() != null)
        {
            now = qu.poll();
            list.add(now);
            if(now.left != null)
            {
                qu.add(now.left);
                nlast = now.left;
            }
            if(now.right != null)
            {
                qu.add(now.right);
                nlast = now.right;
            }
            if(now == last)
            {
                for(int i = 0; i<list.size()-1;i++)
                {
                    list.get(i).next = list.get(i+1);
                }
                list.clear();
                last = nlast;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值