LeetCode(116) Populating Next Right Pointers in Each Node

来源:互联网 发布:我国历年gdp数据 编辑:程序博客网 时间:2024/05/17 23:32

深度优先搜索

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