acwing-week-3-树-Leetcode-98,94,101,105,102,236,543,124,173,297

目录

Leetcode-98

Leetcode-94

Leetcode-101

Leetcode-105

Leetcode-102

Leetcode-236

Leetcode-543

Leetcode-124

Leetcode-173

Leetcode-297


Leetcode-98

/**
 * 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 isValidBST(TreeNode* root) {
        return dfs(root, INT_MIN, INT_MAX);
    }

    bool dfs(TreeNode *root, long long minv, long long maxv){
    	if(!root) return true;
    	if(root -> val < minv || root -> val > maxv) return false;

    	return dfs(root -> left, minv, root -> val -1ll) && dfs(root -> right, root -> val + 1ll, maxv);
    }
};

Leetcode-94

/**
 * 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> inorderTraversal(TreeNode* root) {
        stack<TreeNode*> stk;
        vector<int> res;

        if(!root) return res;
        auto p = root;
        while(p || stk.size()){
        	while(p){
        		stk.push(p);
        		p = p -> left;
        	}
        	p = stk.top();
        	stk.pop();
        	res.push_back(p -> val);
        	p = p -> right;
        }
        return res;
    }
};

Leetcode-101

/**
 * 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 isSymmetric(TreeNode* root) {
    	if(!root) return true;
        return dfs(root -> left, root -> right);
    }

    bool dfs(TreeNode *p, TreeNode * q){
    	if(!p || !q) return !p && !q;
    	return p -> val == q -> val && dfs(p -> left, q -> right) && dfs(p -> right, q -> left);
    }
};

// 迭代
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
    	if(!root) return true;

    	stack<TreeNode*> left, right;
    	auto l = root -> left, r = root -> right;
    	while(l || r || left.size() || right.size()){
    		while(l && r){
    			left.push(l), l = l -> left;
    			right.push(r), r = r -> right;
    		}
    		if(l || r) 
				return false;
			l = left.top(), left.pop();
			r = right.top(), right.pop();
			if(l -> val != r -> val)
				return false;
			l = l -> right, r = r -> left;
    	}
    	return true;
    }
};

Leetcode-105

/**
 * 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:
	unordered_map<int, int> pos;  // 建立哈希表

    TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
		int n = preorder.size();
		for(int i = 0; i < n; i ++)
			pos[inorder[i]] = i;        
		return dfs(preorder, inorder, 0, n - 1, 0, n - 1);
    }

    TreeNode* dfs(vector<int> &preorder, vector<int> &inorder, int pl, int pr, int il,int ir){
    	if(pl > pr) return NULL;
    	int val = preorder[pl];
    	int k = pos[val];
    	int len = k - il;
    	auto root = new TreeNode(val);
    	root -> left = dfs(preorder, inorder, pl + 1, pl + len, il, k - 1);
    	root -> right = dfs(preorder, inorder, pl + len + 1, pr, k + 1, ir);
    	return root; 
    }
};

Leetcode-102

/**
 * 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<vector<int>> levelOrder(TreeNode* root) {
   		vector<vector<int>> res;

   		if(!root) return res;

   		queue<TreeNode*> q;
   		q.push(root);

   		while(q.size()){
   			int len = q.size();
   			vector<int> level;

   			for(int i = 0; i < len; i++){
   				auto t =q.front();
   				q.pop();
   				level.push_back(t -> val);
   				if(t -> left) q.push(t -> left);
   				if(t -> right) q.push(t -> right);
   			}

   			res.push_back(level);
   		}      
   		return res;
    }
};

Leetcode-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) {
        if(!root || root == p || root == q) return root;

        auto left = lowestCommonAncestor(root -> left, p, q);
        auto right = lowestCommonAncestor(root -> right, p, q);

        if(!left) return right;
        if(!right) return left;

        return root;
    }
};

// class Solution{
// public:
// 	TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode *p, TreeNode * q){
// 		std::vector<TreeNode*> path;
// 		std::vector<TreeNode*> node_p_path;
// 		std::vector<TreeNode*> node_q_path;
// 		int finish = 0;
// 		preorder(root, p, path, node_p_path, finish);
// 		path.clear();
// 		finish = 0;
// 		preorder(root, q, path, node_q_path, finish);
// 		int path_len = 0;
// 		if(node_p_path.size() < node_q_path.size()){
// 			path_len = node_p_path.size();
// 		}
// 		else{
// 			path_len = node_q_path.size();
// 		}
// 		TreeNode *result = 0;
// 		for(int i=0; i<path_len; i++){
// 			if(node_p_path[i] == node_q_path[i]){
// 				result = node_p_path[i];
// 			}
// 		}
// 		return result;
// 	}
// private:
// 	void preorder(TreeNode *node, TreeNode* search,std::vector<TreeNode*> &path,
// 		std::vector<TreeNode*> &result, int &finish){
// 		if(!node || finish){
// 			return;
// 		}
// 		path.push_back(node);
// 		if(node == search){
// 			finish = 1;
// 			result = path;
// 		}
// 		preorder(node->left, search, path, result, finish);
// 		preorder(node->right,search, path, result, finish);
// 		path.pop_back();
// 	}
// };

Leetcode-543

/**
 * 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 ans = 0;
    int diameterOfBinaryTree(TreeNode* root) {
        dfs(root);
        return ans;
    }

    int dfs(TreeNode* root){
    	if(!root) return 0;
    	auto left = dfs(root -> left);
    	auto right = dfs(root -> right);
    	ans = max(ans, left + right);;
    	return max(left + 1, right + 1);
    }
};

Leetcode-124


 /** 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 ans = INT_MIN;
    int maxPathSum(TreeNode* root) {
        dfs(root);
        return ans;
    }

    int dfs(TreeNode* root){
    	if(!root) return 0;

    	auto left = dfs(root -> left);
    	auto right = dfs(root -> right);
    	ans = max(ans, left + right + root -> val);
    	return max(0, root -> val + max(left, right));
    }
};

Leetcode-173

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class BSTIterator {
public:

	stack<TreeNode*> stk;

    BSTIterator(TreeNode* root) {
    	while(root){
    		stk.push(root);
    		root = root -> left;
    	}    
    }
    
    /** @return the next smallest number */
    int next() {
        auto p = stk.top();
        stk.pop();
        int res = p -> val;
        p = p -> right;
        while(p){
        	stk.push(p);
        	p = p -> left;
        }
        return res;
    }
    
    /** @return whether we have a next smallest number */
    bool hasNext() {
        return !stk.empty();
    }
};

/**
 * Your BSTIterator object will be instantiated and called as such:
 * BSTIterator* obj = new BSTIterator(root);
 * int param_1 = obj->next();
 * bool param_2 = obj->hasNext();
 */

Leetcode-297

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Codec {
public:

    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
        string res;

        dfs1(root, res); // 用传引用的方式,以减小时间复杂度
        return res;
    }

    void dfs1(TreeNode* root, string &res){
    	if(!root){
    		res += "#,";
    		return;
    	}

    	res += to_string(root -> val) + ',';
    	dfs1(root -> left, res);
    	dfs1(root -> right, res);
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
        int u = 0;
        return dfs2(data, u);
    }

    TreeNode* dfs2(string &data, int &u){
    	if(data[u] == '#'){
    		u += 2;
    		return NULL;
    	}
    	int t = 0;
    	bool is_minus = false;  // 判断是否是负数
    	if(data[u] == '-'){
    		is_minus = true;
    		u++;
    	}
    	while(data[u] != ','){
    		t = t * 10 + data[u] - '0';
    		u++;
    	}

    	u++;

    	if(is_minus) t = -t;

    	auto root = new TreeNode(t);
    	root -> left = dfs2(data, u);
    	root -> right = dfs2(data, u);
    	return root;
    }
};

// Your Codec object will be instantiated and called as such:
// Codec codec;
// codec.deserialize(codec.serialize(root));

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SQLAlchemy 是一个 SQL 工具包和对象关系映射(ORM)库,用于 Python 编程语言。它提供了一个高级的 SQL 工具和对象关系映射工具,允许开发者以 Python 类和对象的形式操作数据库,而无需编写大量的 SQL 语句。SQLAlchemy 建立在 DBAPI 之上,支持多种数据库后端,如 SQLite, MySQL, PostgreSQL 等。 SQLAlchemy 的核心功能: 对象关系映射(ORM): SQLAlchemy 允许开发者使用 Python 类来表示数据库表,使用类的实例表示表中的行。 开发者可以定义类之间的关系(如一对多、多对多),SQLAlchemy 会自动处理这些关系在数据库中的映射。 通过 ORM,开发者可以像操作 Python 对象一样操作数据库,这大大简化了数据库操作的复杂性。 表达式语言: SQLAlchemy 提供了一个丰富的 SQL 表达式语言,允许开发者以 Python 表达式的方式编写复杂的 SQL 查询。 表达式语言提供了对 SQL 语句的灵活控制,同时保持了代码的可读性和可维护性。 数据库引擎和连接池: SQLAlchemy 支持多种数据库后端,并且为每种后端提供了对应的数据库引擎。 它还提供了连接池管理功能,以优化数据库连接的创建、使用和释放。 会话管理: SQLAlchemy 使用会话(Session)来管理对象的持久化状态。 会话提供了一个工作单元(unit of work)和身份映射(identity map)的概念,使得对象的状态管理和查询更加高效。 事件系统: SQLAlchemy 提供了一个事件系统,允许开发者在 ORM 的各个生命周期阶段插入自定义的钩子函数。 这使得开发者可以在对象加载、修改、删除等操作时执行额外的逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值