您的位置:首页 > Web前端 > Node.js

LeetCode(116) Populating Next Right Pointers in Each Node

2015-08-06 21:28 615 查看
深度优先搜索

[code]/**
 * 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);

    }
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: