原题:Populating Next Right Pointers in Each Node
简单的链表二叉树增加Next节点信息,没什么坑。不过还是WA了两次,还是有点菜,继续做,另外leetcode一共150题,考虑两周做完吧,要加速了。
注意:看清楚左右和递归,像标题说的,Go_deep吧。
/**
* 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; if (root->left != NULL)
{
root->left->next = root->right; TreeLinkNode *go_deep_left = root->left->right;
TreeLinkNode *go_deep_right = (root->right != NULL)?(root->right->left):NULL; while (go_deep_left != NULL && go_deep_right != NULL)
{
go_deep_left->next = go_deep_right; go_deep_left = go_deep_left->right;
go_deep_right = go_deep_right->left;
}
} connect(root->right);
connect(root->left);
}
};