二叉树专题(二)

100. Same Tree

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);
    }
};

404. Sum of Left Leaves

中序遍历,判断左子树为叶子结点,累加

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);
    }
};

108.将数组转为二叉搜索树

给定一个数组,其中元素按升序排序,将其转换为高度平衡的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;
    }
};

563.二叉树倾斜度

给定二叉树,求整颗二叉树的倾斜度

树节点的倾斜被定义为所有左子树节点值的总和与所有右子树节点值的总和之间的绝对差。空节点倾斜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;
    }
};

 543.二叉树直径

给定二叉树,计算树的直径长度。二叉树的直径是树中任意两个节点之间最长路径的长度。此路径可能不会通过根。

后序遍历 

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;
        
    }
};

257. Binary Tree Paths

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);
    }
};

671. Second Minimum Node In a Binary Tree

每棵树有两个子节点或没有子节点,如果有两个,该节点的值为两个子节点的较小值

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);
    }
};

101. Symmetric Tree

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);
    }
};

LeetCode 270. Closest Binary Search Tree Value

二叉搜索树最接近值查找

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;
            }
        }
    }
};

235. Lowest Common Ancestor of a Binary Search Tree

最近公共祖先

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;
    }
};

437. Path Sum III

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);
    }
};

572. Subtree of Another Tree

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);
    }
};

501. Find Mode in Binary Search Tree

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)));
    }
};

112. Path Sum

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;
    }
};

 687. Longest Univalue Path

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;
    }
};

猜你喜欢

转载自blog.csdn.net/behboyhiex/article/details/82821009
今日推荐