leetcode Populating Next Right Pointers in Each Node

来源:互联网 发布:剑侠情缘手游工资算法 编辑:程序博客网 时间:2024/06/05 05:02

题目链接

思路:这个题目有问题。其实深度优先并不是常量空间复杂度的。

遇到问题:
1注意链接
root->right->next= root->next->left;
2注意在虽有边的时候root没有next

/** * 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||root->left==NULL)        {            return;        }        root->left->next=root->right;        if(root->right!=NULL&&root->next!=NULL)        {            root->right->next= root->next->left;        }        connect(root->left);        connect(root->right);    }};
0 0
原创粉丝点击