二叉樹專題(二)
阿新 • • 發佈:2018-12-11
class Solution {
public:
bool isSameTree(TreeNode* p, TreeNode* q) {
if(!p || !q) return p==q;
return p->val==q->val && isSameTree(p->left,q->left) && isSameTree(p->right,q->right);
}
};
中序遍歷,判斷左子樹為葉子結點,累加
class Solution { public: int sum=0; int sumOfLeftLeaves(TreeNode* root) { inorder(root); return sum; } void inorder(TreeNode* root){ if(!root) return ; inorder(root->left); if(root->left){ if(!root->left->left && ! root->left->right) sum+=root->left->val; } inorder(root->right); } };
給定一個數組,其中元素按升序排序,將其轉換為高度平衡的BST,每個節點的兩個子樹的深度相差不超過1。
class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(nums.size()==0) return NULL; TreeNode* head=help(nums,0,nums.size()-1); return head; } TreeNode* help(vector<int>& nums,int start,int end){ if(start>end) return NULL; int mid=(start+end)/2; TreeNode *node =new TreeNode(nums[mid]); node->left=help(nums,start,mid-1); node->right=help(nums,mid+1,end); return node; } };
給定二叉樹,求整顆二叉樹的傾斜度
樹節點的傾斜被定義為所有左子樹節點值的總和與所有右子樹節點值的總和之間的絕對差。空節點傾斜0。
整棵樹的傾斜度定義為所有節點傾斜的總和。
後序遍歷
class Solution { public: int res=0; int findTilt(TreeNode* root) { postorder(root); return res; } int postorder(TreeNode* root){ if(!root) return 0; int left=postorder(root->left); int right=postorder(root->right); res+=abs(left-right); return left+right+root->val; } };
給定二叉樹,計算樹的直徑長度。二叉樹的直徑是樹中任意兩個節點之間最長路徑的長度。此路徑可能不會通過根。
後序遍歷
class Solution {
public:
int ans;
int diameterOfBinaryTree(TreeNode* root) {
dfs(root);
return ans;
}
int dfs(TreeNode* root){
if(!root) return 0;
int left=dfs(root->left);
int right=dfs(root->right);
ans=max(ans,left+right);
return max(left,right)+1;
}
};
107.二叉樹層序遍歷II
自底向上
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> res;
if(!root) return res;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()){
int level=q.size();
vector<int> tmp;
for(int i=0;i<level;i++){
root=q.front();
q.pop();
if(root->left) q.push(root->left);
if(root->right) q.push(root->right);
tmp.push_back(root->val);
}
res.push_back(tmp);
}
reverse(res.begin(),res.end());
return res;
}
};
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root)
search(root,"",res);
return res;
}
void search(TreeNode* root,string path,vector<string> &res){
if(!root->left && !root->right){
path+=to_string(root->val);
res.push_back(path);
}
if(root->left) search(root->left,path+to_string(root->val)+"->",res);
if(root->right) search(root->right,path+to_string(root->val)+"->",res);
}
};
每棵樹有兩個子節點或沒有子節點,如果有兩個,該節點的值為兩個子節點的較小值
class Solution {
public:
int findSecondMinimumValue(TreeNode* root) {
if(!root) return -1;
int ans=dfs(root,root->val);
return ans;
}
int dfs(TreeNode* root,int first){
if(!root) return -1;
if(root->val!=first) return root->val;
int left=dfs(root->left,first);
int right=dfs(root->right,first);
if(left==-1) return right;
if(right==-1) return left;
return min(left,right);
}
};
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root) return true;
return help(root->left,root->right);
}
bool help(TreeNode* p,TreeNode* q){
if(!p||!q) return !p&&!q;
if(p->val!=q->val) return false;
return help(p->left,q->right) && help(p->right,q->left);
}
};
二叉搜尋樹最接近值查詢
class Solution {
public:
int closestValue(TreeNode* root, double target) {
if(root->val==target) return root->val;
if(root->val<target){
if(!root->right) return root->val;
else{
int right=closestValue(root->right,target);
return abs(right-target)<abs(root->val-target)?right:root->val;
}
}
else{
if(!root->left) return root->val;
else{
int left=closestValue(root->left,target);
return abs(left-target)<abs(root->val-target)?left:root->val;
}
}
}
};
最近公共祖先
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root->val>p->val && root->val>q->val)
return lowestCommonAncestor(root->left,p,q);
if(root->val<p->val && root->val<q->val)
return lowestCommonAncestor(root->right,p,q);
return root;
}
};
class Solution {
public:
int pathSum(TreeNode* root, int sum) {
if(!root) return 0;
return pathSumfrom(root,sum)+pathSum(root->left,sum)+pathSum(root->right,sum);
}
int pathSumfrom(TreeNode* node, int sum){
if(!node) return 0;
return (node->val==sum?1:0)+ pathSumfrom(node->left,sum-node->val) + pathSumfrom(node->right,sum-node->val);
}
};
class Solution {
public:
bool isSubtree(TreeNode* s, TreeNode* t) {
if(!s) return false;
if(issame(s,t)) return true;
return isSubtree(s->left,t) || isSubtree(s->right,t);
}
bool issame(TreeNode* s, TreeNode* t){
if(!s || !t) return !s && !t;
if(s->val!=t->val) return false;
return issame(s->left,t->left) && issame(s->right,t->right);
}
};
class Solution {
public:
vector<int> findMode(TreeNode* root) {
unordered_map<int,int> map;
vector<int> res;
int ModeCount=getModeCount(root,map);
for(pair<int,int> p:map){
if(p.second==ModeCount)
res.push_back(p.first);
}
return res;
}
int getModeCount(TreeNode* root,unordered_map<int,int>& map){
if(!root) return 0;
if(map.find(root->val)==map.end()){
map.insert(pair<int,int>(root->val,1));
}
else map[root->val]++;
return max(map[root->val],max(getModeCount(root->left,map),getModeCount(root->right,map)));
}
};
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if(!root) return false;
if(!root->left && !root->right && root->val==sum) return true;
return hasPathSum(root->left,sum-root->val)||hasPathSum(root->right,sum-root->val);
}
};
111. Minimum Depth of Binary Tree
class Solution {
public:
int minDepth(TreeNode* root) {
if(!root) return 0;
if(!root->left) return minDepth(root->right)+1;
if(!root->right) return minDepth(root->left)+1;
return min(minDepth(root->left),minDepth(root->right))+1;
}
};
class Solution {
public:
int len=0;
int longestUnivaluePath(TreeNode* root) {
if(!root) return 0;
getlen(root,root->val);
return len;
}
int getlen(TreeNode* root,int val) {
if(!root) return 0;
int left=getlen(root->left,root->val);
int right=getlen(root->right,root->val);
len=max(len,left+right);
if(val==root->val) return max(left,right)+1;
return 0;
}
};