acwing 49. 二叉搜索樹與雙向鏈表

地址:https://www.acwing.com/problem/content/87/node

輸入一棵二叉搜索樹,將該二叉搜索樹轉換成一個排序的雙向鏈表。spa

要求不能建立任何新的結點,只能調整樹中結點指針的指向。3d

注意:指針

  • 須要返回雙向鏈表最左側的節點。

例如,輸入下圖中左邊的二叉搜索樹,則輸出右邊的排序雙向鏈表。code

 

解法blog

樹的處理 一半都是遞歸  分爲 根  樹的左子樹 和樹的右子樹排序

子樹也是一棵樹 進行遞歸處理  向上返回一個雙鏈表  返回鏈表的頭尾遞歸

最後所有轉化鏈表get

代碼it

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    
    TreeNode* rethead = NULL;

    TreeNode* gleft = NULL;
    TreeNode* gright = NULL;
    
    
    void convertInner(TreeNode* root)
    {
        if (NULL == root) return;
    
        if (root->val < rethead->val) rethead = root;
    
        if (root->left == NULL && root->right == NULL) {
            gleft = root; gright = root;
            return;
        }
        else if (root->left != NULL && root->right == NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
            gright = root;
        }
        else if (root->right != NULL && root->left == NULL) {
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
            gleft = root;
        }
        else if (root->right != NULL && root->left != NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
            
            TreeNode* leftcopy = gleft;
    
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
            gleft = leftcopy;
        }
    }
    
    
    
    TreeNode* convert(TreeNode* root) {
        if (NULL == root) return NULL;
        rethead = root;
    
        if (root->left == NULL && root->right == NULL) return root;
    
        if (root->left != NULL && root->right == NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
        }
        else if (root->right != NULL && root->left == NULL) {
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
        }
        else if (root->right != NULL && root->left != NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
    
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
        }
    
        return rethead;
    }
    
};
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    
    TreeNode* rethead = NULL;

    TreeNode* gleft = NULL;
    TreeNode* gright = NULL;
    
    
    void convertInner(TreeNode* root)
    {
        if (NULL == root) return;
    
        if (root->val < rethead->val) rethead = root;
    
        if (root->left == NULL && root->right == NULL) {
            gleft = root; gright = root;
            return;
        }
        else if (root->left != NULL && root->right == NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
            gright = root;
        }
        else if (root->right != NULL && root->left == NULL) {
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
            gleft = root;
        }
        else if (root->right != NULL && root->left != NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
            
            TreeNode* leftcopy = gleft;
    
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
            gleft = leftcopy;
        }
    }
    
    
    
    TreeNode* convert(TreeNode* root) {
        if (NULL == root) return NULL;
        rethead = root;
    
        if (root->left == NULL && root->right == NULL) return root;
    
        if (root->left != NULL && root->right == NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
        }
        else if (root->right != NULL && root->left == NULL) {
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
        }
        else if (root->right != NULL && root->left != NULL) {
            convertInner(root->left);
            gright->right = root;
            root->left = gright;
    
            convertInner(root->right);
            gleft->left = root;
            root->right = gleft;
        }
    
        return rethead;
    }
    
};
相關文章
相關標籤/搜索