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; }};
阅读全文
0 0
- 669. Trim a Binary Search Tree/110. Balanced Binary Tree/443. String Compression/266. Palindrome Per
- leetcode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- LeetCode 669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- leetcode: 669. Trim a Binary Search Tree
- <LeetCode>669. Trim a Binary Search Tree
- leetcode 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- LeetCode 669. Trim a Binary Search Tree
- [LeetCode] 669.Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- 669. Trim a Binary Search Tree
- POJ-1050-To the Max-二维最大子序列和
- Python--错误总结1
- 从零开发B/S机架应用程序(五)
- 关于MatConvNet深度学习库Index exceeds matrix dimensions错误注意事项
- C++信息学奥赛一本通题库1034计算三角形面积
- 669. Trim a Binary Search Tree/110. Balanced Binary Tree/443. String Compression/266. Palindrome Per
- 2017.11.21 LeetCode
- 接口练习2
- orderfields
- JavaScript获取DOM元素的方法总结
- C++标准转换运算符const_cast
- ubuntu常用功能安装集锦
- 机器学习_谱聚类
- BZOJ2829: 信用卡凸包(圆的凸包)