116. Populating Next Right Pointers in Each Node

Total Accepted: 85859  Total Submissions: 235338  Difficulty: Medium

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

Subscribe to see which companies asked this question

Hide Tags
  Tree Depth-first Search
Hide Similar Problems
  (H) Populating Next Right Pointers in Each Node II (M) Binary Tree Right Side View

分析:

这个问题提示用深度优先搜索,但是显然可以用广度搜索来做啊,具体见代码,思路很清晰.

/**
 * 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;
        queue<TreeLinkNode *> que;
        que.push(root);
        root->next=NULL;
        //广度优先的顺序建立连接
        while(!que.empty())
        {
            int levelNum = que.size();//通过size来判断当层的结束 
            vector<TreeLinkNode *> curlevelNodes;  
            //一,获取当层的所有节点
            for(int i=0; i<levelNum; i++)   
            {  
                if(que.front()->left != NULL) //先获取该节点下一层的左右子,再获取该节点的元素,因为一旦压入必弹出,所以先处理左右子  
                    que.push(que.front()->left);  
                if(que.front()->right != NULL)   
                    que.push(que.front()->right);  
                      
                curlevelNodes.push_back(que.front());  
                que.pop();  
            } 
            //二,将当层建立连接
            for(int i=0; i<levelNum-1; i++)   
                curlevelNodes[i]->next=curlevelNodes[i+1];
            //最后一个节肯定为NULL撒!    
            curlevelNodes[levelNum-1]->next=NULL;    
        }
        return;
    }
};


学习别人算法:

卧槽,简洁明了,不错!

/**
 * 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 || !root->left)  
            return;
        help(root->left, root->right);
    }

    void help(TreeLinkNode* a, TreeLinkNode* b){
        a->next=b;
        if(a->left){
            help(a->left, a->right);
            help(a->right, b->left);
            help(b->left, b->right);
        }
    }
};


注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51228929

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

你可能感兴趣的:(LeetCode,C++,算法,面试,搜索)