669. Trim a Binary Search Tree/110. Balanced Binary Tree/443. String Compression/266. Palindrome Per

来源:互联网 发布:欧洲人怎么看中国知乎 编辑:程序博客网 时间:2024/05/21 19:38

669. Trim a Binary Search Tree

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.Example 1:Input:     1   / \  0   2  L = 1  R = 2Output:     1      \       2Example 2:Input:     3   / \  0   4   \    2   /  1  L = 1  R = 3Output:       3     /    2     / 1

methods:

/** * 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* trimBST(TreeNode* root, int L, int R) {        if(!root) {            return NULL;        }        if(root->val < L) {            return trimBST(root->right, L, R);        }        if(root->val > R) {            return trimBST(root->left, L, R);        }        root->left = trimBST(root->left, L, R);        root->right = trimBST(root->right, L, R);        return root;    }};

above methods have the problem in memory leakage.

/** * 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* trimBST(TreeNode* root, int L, int R, bool top = true) {        if(!root) {            return NULL;        }        root->left = trimBST(root->left, L, R, false);        root->right = trimBST(root->right, L, R, false);        if(root->val >= L && root->val <= R) {            return root;        }        TreeNode* new_root = root->val < L ? root->right: root->left;        if(!top) {            delete root;        }        return new_root;    }};

Here the order to get left and right node is diff from the first methods. As we need to delete the trim nodes recursively.

266. Palindrome Permutation

Given a string, determine if a permutation of the string could form a palindrome.For example,"code" -> False, "aab" -> True, "carerac" -> True.
class Solution {public:    bool canPermutePalindrome(string s) {        int len = s.size();        map<char, int> mmp;        for(int idx = 0; idx < len; idx++) {            if(mmp.count(s[idx]) > 0) {                mmp[s[idx]]++;            }            else {                mmp[s[idx]] = 1;            }        }        int isPer = 0;        for(map<char, int>::iterator it = mmp.begin(); it != mmp.end(); it++) {            if((it->second)%2) {                isPer++;            }        }        return isPer <= 1;    }};

443. String Compression

Given an array of characters, compress it in-place.The length after compression must always be smaller than or equal to the original array.Every element of the array should be a character (not int) of length 1.After you are done modifying the input array in-place, return the new length of the array.
class Solution {public:    int compress(vector<char>& chars) {        int comLen = 0;        int numCh = chars.size();        if(!numCh) {            return 0;        }        char lastCh = chars[0];        int rec = 1;        for(int idx = 1; idx < numCh; idx++) {            if(lastCh == chars[idx]) {                rec++;            }                else {                if(rec > 1) {                    chars[comLen] = lastCh;                    comLen++;                    char str[1000];                    sprintf(str, "%d", rec);                     for(int idx2 = 0; idx2 < strlen(str); idx2++) {                        chars[comLen] = str[idx2];                        comLen++;                    }                }                else {                    chars[comLen] = lastCh;                    comLen++;                }                rec = 1;            }            lastCh = chars[idx];        }        if(rec > 1) {            chars[comLen] = lastCh;            comLen++;            char str[1000];            sprintf(str, "%d", rec);             for(int idx2 = 0; idx2 < strlen(str); idx2++) {                chars[comLen] = str[idx2];                comLen++;            }        }        else {            chars[comLen] = lastCh;            comLen++;        }        return comLen;    }};

110. Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

/** * 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:    struct result {        bool isB;        int depth;    };    struct result getDepth(TreeNode* root) {        if(!root) {            struct result res;            res.isB = true;            res.depth = 0;            return res;        }        struct result isLeft = getDepth(root->left);        if(!isLeft.isB) {            return isLeft;        }        struct result isRight = getDepth(root->right);        if(!isRight.isB) {            return isRight;        }        if(abs(isLeft.depth - isRight.depth) > 1) {            struct result res;            res.isB = false;            return res;                   }        else {            struct result res;            res.isB = true;            res.depth = isLeft.depth > isRight.depth ? isLeft.depth:isRight.depth;            res.depth++;            return res;        }    }    bool isBalanced(TreeNode* root) {        struct result res = getDepth(root);        return res.isB;    }};
原创粉丝点击