(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path...

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第1张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第2张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第3张图片

 

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: the maximum weight node
     */
    int max_sum;
    TreeNode* max_node;
    
    TreeNode * findSubtree(TreeNode * root) {
        // write your code here
        if(root==NULL)
            return root;
        if(root->left==NULL && root->right==NULL)
            return root;
        max_sum = INT_MIN;
        max_node = NULL;
        dfs(root);
        return max_node;
    }
    int dfs(TreeNode* cur){
        if(cur==NULL)
            return 0;
        int sum = dfs(cur->left)+dfs(cur->right)+cur->val;
        if(sum > max_sum){
            max_sum = sum;
            max_node = cur;
        }
        return sum;
    }
    
};

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第4张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第5张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第6张图片

 

 计算每个结点的高度并用一个map来存储对应的值,然后将高度从1开始到max_depth 逐层打印出来。

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param root: the root of binary tree
     * @return: collect and remove all leaves
     */
    int max_depth;
    unordered_map<int, vector<int>> depth;
    
    int dfs(TreeNode* cur){
        if(cur == NULL)
            return 0;
        int d = max(dfs(cur->left), dfs(cur->right))+1;
        max_depth = max(max_depth, d);
        depth[d].push_back(cur->val);
        return d;
    }
    vectorint>> findLeaves(TreeNode * root) {
        // write your code here
        vectorint>> ans;
        max_depth = 0;
        dfs(root);
        for(int i=1; i<=max_depth; i++)
            ans.push_back(depth[i]);
        return ans;
    }
};

 

二叉树翻转:

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第7张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第8张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第9张图片

 

 

 

 

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of binary tree
     * @return: new root
     */
    TreeNode* newRoot;
    TreeNode * upsideDownBinaryTree(TreeNode * root) {
        // write your code here
        if(root == NULL)
            return root;
        dfs(root);
        return newRoot;
    }
    void dfs(TreeNode* cur){
        if(cur->left != NULL){
            dfs(cur->left);
            cur->left->right = cur;
            cur->left->left = cur->right;
            cur->left = NULL;
            cur->right = NULL;
        }
        else
            newRoot = cur;
    }
};

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第10张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第11张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第12张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第13张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第14张图片

 

 

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */

class Solution {
public:
    /**
     * @param root: the root of tree
     * @return: the vertical order traversal
     */
    vectorint>> verticalOrder(TreeNode * root) {
        // write your code here
        vectorint>> ans;
        unordered_map<int, vector<int>> col;
        queue<int> q1;   //列数进队列
        queue q2;   //结点进队列
        int col_min = 0, col_max = 0;   //记录列数的最小值和最大值
        if(root == NULL)
            return ans;
        
        q1.push(0);
        q2.push(root);
        
        //BFS
        while(!q1.empty()){
            int c = q1.front();
            TreeNode* node = q2.front();
            q1.pop();
            q2.pop();
            
            //将对应层数的node值放进map中
            col[c].push_back(node->val);
            
            col_min = min(col_min, c);
            col_max = max(col_max, c);
            
            if(node->left){
                q1.push(c-1);
                q2.push(node->left);
            }
            if(node->right){
                q1.push(c+1);
                q2.push(node->right);
            }
        }
        
        for(int i=col_min; i<=col_max; i++){
            ans.push_back(col[i]);
        }
        
        return ans;
    }
};

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第15张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第16张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第17张图片

 

 

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第18张图片

 

 

/**
 * 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:
    /*
     * @param root: The root of the BST.
     * @param p: You need find the successor node of p.
     * @return: Successor of p.
     */
    TreeNode * inorderSuccessor(TreeNode * root, TreeNode * p) {
        // write your code here
        if(root==NULL || p==NULL)
            return NULL;
        
        TreeNode* ans = NULL, *cur = root;
        
        //case 1,2 
        //case1 root

//case2 root>p : ans 可能为root或root的左子树中 while(cur!=p){ if(p->val < cur->val){ ans = cur; cur = cur->left; } if(p->val > cur->val) cur = cur->right; } //case 3 root==p : if p有右子树, then ans is 右子树中最小的结点 if(p->right != NULL){ cur = p->right; while(cur->left != NULL) cur = cur->left; ans = cur; } return ans; } };

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第19张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第20张图片

 

 返回一颗二叉树中,值相同的结点的路径。

参考连接:http://zxi.mytechroad.com/blog/tree/leetcode-687-longest-univalue-path/

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第21张图片

 

 

/**
 * 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:
    int longestUnivaluePath(TreeNode* root) {
        if(root == NULL)
            return 0;
        int ans = 0;
        univaluePath(root, ans);
        return ans;
    }
    int univaluePath(TreeNode* root, int& ans){
        if(root == NULL)
            return 0;
        int l = univaluePath(root->left, ans);
        int r = univaluePath(root->right, ans);
        int pl = 0;
        int pr = 0;
        if(root->left && root->val == root->left->val)
            pl = l+1;
        if(root->right && root->val == root->right->val)
            pr= r+1;
        ans = max(ans, pl+pr);
        return max(pl, pr);
    }
};

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第22张图片

 

 

/**
 * 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:
    bool hasPathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return false;
        if(root->left == NULL && root->right == NULL)
            return root->val == sum;
        int new_sum = sum - root->val;
        return hasPathSum(root->left, new_sum) || hasPathSum(root->right, new_sum);
    }
};

 

(BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第23张图片

 

 (BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path..._第24张图片

 

 

/**
 * 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:
    vectorint>> pathSum(TreeNode* root, int sum) {
        vectorint>> ans;
        vector<int> curr;
        pathSum(root, sum, curr, ans);
        return ans;
    }
    
    void pathSum(TreeNode* root, int sum, vector<int>& curr, vectorint>>& ans){
        if(root == NULL)
            return;
        if(root->left==NULL && root->right==NULL){
            if(root->val == sum){
                //ans.push_back(curr);
                //ans.back().push_back(root->val);
                curr.push_back(root->val);
                ans.push_back(curr);
                curr.pop_back();
            }
            // root->val != sum
            return;
        }
        curr.push_back(root->val);
        int new_sum = sum - root->val;
        pathSum(root->left, new_sum, curr, ans);
        pathSum(root->right, new_sum, curr, ans);
        curr.pop_back();
    }
};

 

/**
 * 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:
    vectorint>> pathSum(TreeNode* root, int sum) {
        vectorint>> ans;
        vector<int> cur;
        path(root, sum, cur, ans);
        return ans; 
    }
    
    void path(TreeNode* root, int sum, vector<int>& cur, vectorint>>& ans){
        if(root == NULL)
            return;
        sum -= root->val;
        if(root->left==NULL && root->right==NULL){
            if(sum == 0){
                cur.push_back(root->val);
                ans.push_back(cur);
                cur.pop_back(); 
            }
            return;
        }
        cur.push_back(root->val);
        path(root->left, sum, cur, ans);
        path(root->right, sum, cur, ans);
        cur.pop_back();
        }
};

 

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
//不需要是root node & leaf node 结尾

class Solution {
public:
    int pathSum(TreeNode* root, int sum) {
        if(root == NULL)
            return 0;
        //root + left + right
        return dfs(root, sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
    }
    
    int dfs(TreeNode* root, int sum){
        if(root == NULL)
            return 0;
        int new_sum = sum - root->val;
        // sum -= root->val;
        return (new_sum==0? 1 : 0)+ dfs(root->left, new_sum) + dfs(root->right, new_sum);
    }
};

 

转载于:https://www.cnblogs.com/Bella2017/p/11451258.html

你可能感兴趣的:((BST, 二叉树,递归) lintcode 628. Maximum Subtree,650. 651. 649. 448. leetcode 687. longest Univalue Path...)