Populating Next Right Pointers in Each Node II

原创 2015年11月20日 16:26:21
  1. 问题

    Follow up for problem "Populating Next Right Pointers in Each Node".

    What if the given tree could be any binary tree? Would your previous solution still work?

    Note:

    • You may only use constant extra space.

    For example,
    Given the following binary tree,

             1
           /  \
          2    3
         / \    \
        4   5    7
    

    After calling your function, the tree should look like:

             1 -> NULL
           /  \
          2 -> 3 -> NULL
         / \    \
        4-> 5 -> 7 -> NULL
    
  2. 解答
    class Solution {
    public:
        void connect(TreeLinkNode *root) 
        {
            queue<TreeLinkNode *> qu;
            if(!root) return ;
            qu.push(root);
            
            root->next=NULL;
            while(!qu.empty())
            {
                int len=qu.size();
                for(int i=0;i<len;i++)
                {
                    TreeLinkNode *temp=qu.front();
                    qu.pop();
                    if(i==len-1) 
                        temp->next=NULL;
                    else
                        temp->next=qu.front();
                    
                    if(temp->left)
                    {
                        qu.push(temp->left);
                    }
                    if(temp->right)
                    {
                        qu.push(temp->right);
                    }
                }
                //TreeLinkNode *temp1=qu.front();
               // temp1->next=NULL;
               // qu.pop();
                
            }
        }
    };


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

leetcode 之Populating Next Right Pointers in Each Node II解题思路

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could...

Populating Next Right Pointers in Each Node II (Java)

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could...

LeetCode刷题笔录Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree could...

leetcode 116:Populating Next Right Pointers in Each Node I And II

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; ...

[LeetCode] Populating Next Right Pointers in Each Node I, II

Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; ...

Leetcode 117. Populating Next Right Pointers in Each Node II 链接邻居2 解题报告

1 解题思想这道题和116基本一样: Leetcode 116. Populating Next Right Pointers in Each Node 链接邻居 解题报告 区别在于这个是任意二叉树...
  • MebiuW
  • MebiuW
  • 2016年09月30日 22:52
  • 1145

Populating Next Right Pointers in Each Node II - LeetCode 117

题目描述: Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tree co...
  • bu_min
  • bu_min
  • 2015年05月23日 20:54
  • 203

LeetCode - Populating Next Right Pointers in Each Node I && II

Populating Next Right Pointers in Each Node I https://leetcode.com/problems/populating-next-right...

LeetCode 117 Populating Next Right Pointers in Each Node II

题目 Given a binary tree struct TreeLinkNode { TreeLinkNode...
  • seawade
  • seawade
  • 2014年03月05日 12:52
  • 536

LeetCode OJ:Populating Next Right Pointers in Each Node II

Populating Next Right Pointers in Each Node II     Follow up for problem "Populating Next Right Po...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Populating Next Right Pointers in Each Node II
举报原因:
原因补充:

(最多只允许输入30个字)