二叉树
Faith★
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
合并二叉树
class Solution { public: TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) { TreeNode* root; if(t1==nullptr&&t2 == nullptr){ return 0; } else if(t1!=nullptr&&t2 == nullptr){ root =.原创 2020-11-26 14:43:58 · 98 阅读 · 0 评论 -
二叉树的直径
class Solution { public: int ans; int diameterOfBinaryTree(TreeNode* root) { ans = 1; dfs(root); return ans - 1; } int dfs(TreeNode* root){ if(root == NULL) return 0; int left = dfs(root->.原创 2020-11-26 14:43:53 · 120 阅读 · 0 评论 -
把二叉搜索树转换为累加树
把二叉搜索树转换为累加树 给出二叉 搜索 树的根节点,该树的节点值各不相同,请你将其转换为累加树(Greater Sum Tree),使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 提醒一下,二叉搜索树满足下列约束条件: 节点的左子树仅包含键 小于 节点键的节点。 节点的右子树仅包含键 大于 节点键的节点。 左右子树也必须是二叉搜索树。 注意:本题和 1038: https://leetcode-cn.com/problems/binary-search-tree-to-.原创 2020-11-26 14:44:04 · 334 阅读 · 0 评论 -
路径总和III
class Solution { public: int count = 0; int pathSum(TreeNode* root, int sum) { if(root == nullptr) return count; dfs(root,sum); pathSum(root->left,sum); pathSum(root->right,sum); return count; .原创 2020-11-26 14:43:49 · 103 阅读 · 0 评论 -
二叉树的最近公共祖先
class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { if(root==NULL) return NULL; if(root == p || root == q) return root; TreeNode *left = lowestCommonAncestor(root-.原创 2020-11-26 14:43:44 · 79 阅读 · 0 评论 -
翻转二叉树
class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root == NULL) return NULL; root->right = invertTree(root->right); root->left = invertTree(root->left); swap(root->left,root->r.原创 2020-11-26 14:43:39 · 89 阅读 · 0 评论 -
二叉树展开为链表
class Solution { public: vector<TreeNode*> v; void dfs(TreeNode* root){ if(root==NULL) return; v.push_back(root); dfs(root->left); dfs(root->right); } void flatten(TreeNode* root) { .原创 2020-11-26 14:43:35 · 87 阅读 · 0 评论 -
二叉树的最大深度
class Solution { public: int maxDepth(TreeNode* root) { if(root == nullptr) return 0; return max(maxDepth(root->left),maxDepth(root->right))+1; } };原创 2020-11-26 14:43:30 · 87 阅读 · 0 评论 -
二叉树的层序遍历
class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>> res; queue<TreeNode*> q; if(root!=NULL) q.push(root); while(!q.empty()){ .原创 2020-11-26 14:43:25 · 182 阅读 · 0 评论 -
对称二叉树
class Solution { public: bool isSymmetric(TreeNode* root) { return isMirror(root,root); } bool isMirror(TreeNode* t1, TreeNode* t2){ if(t1==nullptr&&t2==nullptr){ return true; } if(t1 == null.原创 2020-11-26 14:43:06 · 71 阅读 · 0 评论 -
验证二叉搜索树
class Solution { public: bool isValidBST(TreeNode* root) { long long pre = LONG_MIN; return isBST(root, pre); } bool isBST(TreeNode* root,long long& pre){ if(root){ if(!isBST(root->left,pre)) .原创 2020-11-26 14:43:01 · 87 阅读 · 0 评论 -
不同的二叉搜索树
class Solution { public: int numTrees(int n) { vector<int> v(n+1,0); v[0] = 1; v[1] = 1; for(int i = 2;i <= n;i++){ for(int j = 1;j <= i;j++){ v[i] += v[j-1]*v[i-j]; }.原创 2020-11-26 14:42:55 · 71 阅读 · 0 评论 -
二叉树的中序遍历
class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> v; traversal(root,v); return v; } void traversal(TreeNode* root,vector<int>& v){ if(root == NULL) .原创 2020-11-26 14:42:49 · 107 阅读 · 0 评论
分享