思路:这个题目有问题。其实深度优先并不是常量空间复杂度的。
遇到问题:
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);
}
};