五、算法分类经典题目练习:二叉树与图

113.路径总和II

/**
 * 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:
    std::vector<std::vector<int> > pathSum(TreeNode* root, int sum) {
     
    	std::vector<std::vector<int> > result;
    	std::vector<int> path;
    	int path_value = 0;
    	preorder(root, path_value, sum, path, result);
    	return result;
    }
private:
	void preorder(TreeNode *node, int &path_value, int sum,
				std::vector<int> &path,
				std::vector<std::vector<int> > &result){
     
		if (!node){
     
			return;
		}
		path_value += node->val;
		path.push_back(node->val);
		if (!node->left && !node->right && path_value == sum){
     
			result.push_back(path);
		}
		preorder(node->left, path_value, sum, path, result);
		preorder(node->right, path_value, sum, path, result);
		path_value -= node->val;
		path.pop_back();
	}
};

236.二叉树的公共祖先

/**
 * 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* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
     
        vector<TreeNode*> path;
        vector<TreeNode*> p_path;
        vector<TreeNode*> q_path;
        int finish = 0;
        preorder(root, p, path, p_path, finish);
        path.clear();
        finish = 0;
        preorder(root, q, path, q_path, finish);
        int len = min(p_path.size(), q_path.size());
        TreeNode* result = NULL;
        for(int i = 0; i < len; ++i){
     
            if(q_path[i]==p_path[i])
                result = q_path[i];
        }
        return result;
    }
    void preorder(TreeNode* node, TreeNode* target, vector<TreeNode*>& path, vector<TreeNode*>& res, int& finish){
     
        if(!node || finish){
     
            return;
        }
        path.push_back(node);
        if(node == target){
     
            res = path;
            finish = 1;
        }
        preorder(node->left, target, path, res, finish);
        preorder(node->right, target, path, res, finish);
        path.pop_back();
    }
};

114.二叉树展开为链表

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
     
public:
    void flatten(TreeNode *root) {
     
        TreeNode *last = NULL;
        preorder(root, last);
    }
private:
	void preorder(TreeNode *node, TreeNode *&last){
     
		if (!node){
     
			return;
		}
		if (!node->left && !node->right){
     
			last = node;
			return;
		}
		TreeNode *left = node->left;
		TreeNode *right = node->right;
		TreeNode *left_last = NULL;
		TreeNode *right_last = NULL;
		if (left){
     
			preorder(left, left_last);
			node->left = NULL;
			node->right = left;
			last = left_last;
		}
		if (right){
     
			preorder(right, right_last);
			if (left_last){
     
				left_last->right = right;
			}
			last = right_last;
		}
	}
};

199.二叉树的右视图

/**
 * 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:
    vector<int> rightSideView(TreeNode* root) {
     
        vector<int> res;
        queue<pair<TreeNode*,int>> q;
        if(root==NULL) return res;
        q.push(make_pair(root,0));
        while(!q.empty()){
     
            TreeNode* p = q.front().first;
            int depth = q.front().second;
            q.pop();
            if(res.size()==depth) res.push_back(p->val);
            else res[depth] = p->val;
            if(p->left) q.push(make_pair(p->left,depth+1));
            if(p->right) q.push(make_pair(p->right,depth+1));
        }
        return res;
    }
};

207.课程表(图)

class GraphNode {
     
public:
    int val;
    vector<GraphNode*> neighbors;
    GraphNode(int val_):val(val_), neighbors(){
     }
};

class Solution {
     
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
     
        vector<GraphNode*> graph;
        vector<int> degree(numCourses,0);
        for(int i = 0; i < numCourses; ++i){
     
            graph.push_back(new GraphNode(i));
        }
        for(int i = 0; i < prerequisites.size(); ++i){
     
            graph[prerequisites[i][0]]->neighbors.push_back(graph[prerequisites[i][1]]);
            degree[prerequisites[i][1]]++;
        }
        queue<GraphNode*> q;
        for(int i = 0; i < numCourses; ++i){
     
            if(degree[i]==0)
                q.push(graph[i]);
        }
        while(!q.empty()){
     
            GraphNode* p = q.front();
            q.pop();
            for(auto g : p->neighbors){
     
                degree[g->val]--;
                if(degree[g->val]==0)
                    q.push(graph[g->val]);
            }
        }
        for(int i = 0; i < numCourses; ++i)
            delete graph[i];
        for(int i = 0; i < numCourses; ++i){
     
            if(degree[i]!=0)
                return false;
        }
        return true;
    }
};

你可能感兴趣的:(算法分类练习,二叉树,算法,leetcode)