222.完全二叉树的节点个数
链接:力扣
利用完全二叉树的性质,这个更容易想到,如何计算完全二叉树的节点数 | labuladong 的算法小抄
另一个二分的方法计算太偏了,既然时间复杂度都是O(logn*logn),不如记忆简单的那个
/** * 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: //层序遍历和深度优先的后序遍历,不过都没有用到二叉树的性质 int countNodes(TreeNode* root) { TreeNode *l=root; int left_level=0; while(l!=nullptr) { left_level++; l=l->left; } TreeNode *r=root; int right_level=0; while(r!=nullptr) { right_level++; r=r->right; } if(right_level==left_level) { return static_cast<int>(pow(2,left_level)-1); } return countNodes(root->left)+countNodes(root->right)+1; } };
110.平衡二叉树
链接:力扣
//唉,又看了一遍代码随想录,我好像总是只能想到自己的解法
/** * 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: bool isBalanced(TreeNode* root) { int result=depth(root); return (result==-1)?false:true; } int depth(TreeNode *root) { if(root==nullptr)return 0; int ld=depth(root->left); if(ld==-1) { return -1; } int rd=depth(root->right); if(rd==-1) { return -1; } if(abs(ld-rd)>1) { return -1; } return 1+max(ld,rd); } };
257. 二叉树的所有路径
链接:力扣
厌倦了
我的做法:
/** * 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: vector<string>v; string s; vector<string> binaryTreePaths(TreeNode* root) { if(root==nullptr) { return v; } backtracing(root,s); return v; } void backtracing(TreeNode *root,string s) { if(root!=nullptr) { if(root->left==nullptr &&root->right==nullptr)//叶子节点 { v.push_back(s+to_string(root->val)); } else { backtracing(root->left,s+to_string(root->val)+"->"); backtracing(root->right,s+to_string(root->val)+"->"); } } } };
100.相同的树
链接:力扣
/** * 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: bool isSameTree(TreeNode* p, TreeNode* q) { if(p==nullptr && q==nullptr) { return true; } if(p!=nullptr && q!=nullptr && p->val==q->val) { bool l=isSameTree(p->left,q->left); bool r=isSameTree(p->right,q->right); return l&&r; } return false; } };
a
链接:
a
a
这道题有意思的地方在于,A和B在判断是否same时,不是完全相同。B遍历结束时A可以不遍历结束。
特殊例子:
这种情况下是true。所以写的函数是is_almostsame而不是is_same
/** * 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 isSubStructure(TreeNode* A, TreeNode* B) { if(A==nullptr || B==nullptr) { return false; } else { return is_almostsame(A,B)||isSubStructure(A->left,B)||isSubStructure(A->right,B); } } bool is_almostsame(TreeNode *p,TreeNode *q) { if(q==nullptr)//这里A可以不用遍历完,只要求B能遍历完 { return true; } if(p!=nullptr &&q!=nullptr && p->val==q->val) { bool l=is_almostsame(p->left,q->left); bool r=is_almostsame(p->right,q->right); return l&&r; } return false; } };
y一开始写错,错的两个点:
404左叶子之和
链接:力扣
/** * 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: int sum=0; int sumOfLeftLeaves(TreeNode* root) { helper(root); return sum; } void helper(TreeNode *root) { if(root!=nullptr) { if(root->left!=nullptr && root->left->left==nullptr && root->left->right==nullptr) { sum+=root->left->val; } helper(root->left); helper(root->right); } } };
513. 找树左下角的值
链接:力扣
递归前序遍历:
/** * 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: int maxdepth=INT_MIN; int result=0;//要找的那个数 int findBottomLeftValue(TreeNode* root) { backtracing(root,0); return result; } void backtracing(TreeNode *root,int temp_depth) { //那么如何找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。 if(root!=nullptr) { if(root->left==nullptr && root->right==nullptr) { if(temp_depth>maxdepth) { maxdepth=temp_depth; result=root->val; } } temp_depth++; backtracing(root->left,temp_depth); backtracing(root->right,temp_depth); temp_depth--; } } };
简单层序遍历:
/** * 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: int findBottomLeftValue(TreeNode* root) { queue<TreeNode*>q; q.push(root); int value=0;//要找的值 while(!q.empty()) { int len=q.size(); for(int i=0;i<len;i++) { TreeNode *temp=q.front(); q.pop(); if(i==0) { value=temp->val; } if(temp->left!=nullptr) { q.push(temp->left); } if(temp->right!=nullptr) { q.push(temp->right); } } } return value; } };
112. 路径总和
链接:力扣
三刷:
/** * 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: //回溯 bool hasPathSum(TreeNode* root, int targetSum) { if(root!=nullptr) { if(root->left==nullptr && root->right==nullptr) { // cout<<targetSum<<endl; return (targetSum==root->val); } else { targetSum-=root->val; bool l=hasPathSum(root->left,targetSum); bool r=hasPathSum(root->right,targetSum); targetSum+=root->val; return l||r; } } return false; } };
106. 从中序与后序遍历序列构造二叉树
链接:力扣
思路明确。
/** * 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: unordered_map<int,int> inorder_umap; TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) { int n=inorder.size(); for(int i=0;i<n;i++) { inorder_umap.insert({inorder[i],i}); } TreeNode *temp=helper(inorder,0,n-1,postorder,0,n-1); return temp; } TreeNode *helper(vector<int>&inorder, int in_start,int in_end, vector<int>&postorder, int p_start,int p_end ) { if(in_start<=in_end && p_start<= p_end) { int rootVal=postorder[p_end]; TreeNode *root=new TreeNode(rootVal); int inoder_index=inorder_umap[rootVal]; int left_length=inoder_index-in_start; int right_length=in_end-inoder_index; root->left=helper(inorder, in_start,inoder_index-1, postorder,p_start,p_start+left_length-1); root->right=helper(inorder,inoder_index+1,in_end, postorder,p_start+left_length,p_end-1); return root; } return nullptr; } };
654. 最大二叉树
链接:力扣
秒了
/** * 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: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { int n=nums.size(); TreeNode *root=helper(nums,0,n-1); return root; } TreeNode *helper(vector<int>& nums,int start,int end) { if(start<=end) { int maxx=nums[start]; int max_index=start; for(int i=start;i<=end;i++) { if(maxx<nums[i]) { maxx=nums[i]; max_index=i; } } TreeNode *root=new TreeNode(maxx); root->left=helper(nums,start,max_index-1); root->right=helper(nums,max_index+1,end); return root; } return nullptr; } };
617. 合并二叉树
链接:力扣
/** * 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: TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) { if(root1==nullptr && root2==nullptr) { return nullptr; } else if(root1==nullptr) { return root2; } else if(root2==nullptr) { return root1; } else { TreeNode *l=mergeTrees(root1->left,root2->left); TreeNode *r=mergeTrees(root1->right,root2->right); root1->val+=root2->val; root1->left=l; root1->right=r; return root1; } } };
700.二叉搜索树中的搜索 ---------不太会
链接:代码随想录
/** * 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: bool find=false; TreeNode* searchBST(TreeNode* root, int val) { if(root==nullptr) { return nullptr; } if(val==root->val) { return root; } else if(val<root->val)//遍历左子树 { return searchBST(root->left,val); } else { return searchBST(root->right,val); } } }; 二、迭代法 */ class Solution { public: bool find=false; TreeNode* searchBST(TreeNode* root, int val) { TreeNode *r=root; while(r!=nullptr) { if(val==r->val) { return r; } else if(val<r->val) { r=r->left; } else { r=r->right; } } return nullptr; } };
98.验证二叉搜索树
链接:代码随想录
/** * 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 { //更简单的想法确实是中序遍历,因为中序遍历一定从小到大,只需要记录当前序列的最大值,和root->val进行比较即可 public: bool isValidBST(TreeNode* root) { long long maxx=LONG_MIN; return helper(root,maxx); } bool helper(TreeNode *root,long long &maxx) { if(root==nullptr) { return true; } bool left= helper(root->left,maxx); if(root->val<=maxx) { return false; } else { maxx=root->val; } bool right =helper(root->right,maxx); return left && right; } };
还有种用指针记录的
/** * 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: TreeNode *pre=nullptr; bool isValidBST(TreeNode* root) { if(root!=nullptr) { bool l=isValidBST(root->left);//中序遍历 if(pre!=nullptr) { if(pre->val>=root->val) { return false; } } pre=root; bool r=isValidBST(root->right);//中序遍历 return l&&r; } return true; } };
530. 二叉搜索树的最小绝对差
链接:力扣
递归:
/** * 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: TreeNode *pre=nullptr; int minn=INT_MAX; int getMinimumDifference(TreeNode* root) { dfs(root); return minn; } void dfs(TreeNode *root) { if(root!=nullptr) { dfs(root->left); if(pre!=nullptr) { minn=min(minn,root->val-pre->val); } pre=root; dfs(root->right); } } };
非递归:
/** * 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: int getMinimumDifference(TreeNode* root) { int minn=10e5+1; stack<TreeNode*>sta; TreeNode *node=root; TreeNode* pre = NULL; while(!sta.empty()|| node!=nullptr) { while(node!=nullptr) { sta.push(node); node=node->left; } node=sta.top(); /*中序遍历位置 cout<<node->val<<endl;*/ if(pre!=nullptr) { minn=min(minn,abs(pre->val-node->val)); } pre=node; sta.pop(); node=node->right; } return minn; } };
501.二叉搜索树中的众数 --------不会,重新看了一遍答案
链接:力扣
/** * 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: int cnt=1; int max_cnt=INT_MIN; TreeNode *pre=nullptr; vector<int>result; vector<int> findMode(TreeNode* root) { inorder(root); return result; } void inorder(TreeNode *root) { if(root!=nullptr) { inorder(root->left); if(pre==nullptr) { cnt=1; } else if(pre->val==root->val) { cnt++; } else { cnt=1; } pre=root; if(cnt>max_cnt) { result.clear(); max_cnt=cnt; result.push_back(root->val); } else if(cnt==max_cnt) { result.push_back(root->val); } inorder(root->right); } } };
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==nullptr) return nullptr; if(root==p || root==q) return root; TreeNode *l=lowestCommonAncestor(root->left,p,q); TreeNode *r=lowestCommonAncestor(root->right,p,q); if(l==nullptr && r==nullptr) { return nullptr; } else if(l==nullptr) { return r; } else if(r==nullptr) { return l; } else { return root; } } };
235. 二叉搜索树的最近公共祖先-------没思路
链接:代码随想录
很值得再做。
/** * 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==nullptr)return nullptr; if(root->val<p->val && root->val<q->val) { return lowestCommonAncestor(root->right,p,q); } else if(root->val>p->val&& root->val>q->val) { return lowestCommonAncestor(root->left,p,q); } else { return root; } } };
701.二叉搜索树中的插入操作
链接:力扣
450.删除二叉搜索树中的节点
链接:力扣
一些错误思路,要找的节点不是父节点,就是要删除的节点
/** * 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: /* 被删除的节点是空、查询不到 被删除的节点是叶子节点 被删除的节点有左子树,右子树为空,直接以root->left替代root 被删除的节点有左右子树,找到右子树的最小节点替代 右子树的最小节点,在以root->right为根节点的左子树的最左边 */ TreeNode *pre=nullptr;//代表要找到节点的父节点 TreeNode* deleteNode(TreeNode* root, int key) { if(root==nullptr)return nullptr; if(root->val==key)//要被删除的节点 { if(root->left==nullptr && root->right==nullptr) { root=nullptr; } else if(root->left!=nullptr && root->right==nullptr) { root=root->left; } else if(root->left==nullptr && root->right!=nullptr) { root=root->right; } else { //找到要被删除节点的右节点,找到右子树的最小节点替代 右子树的最小节点,在以root->right为根节点的左子树的最左边 TreeNode *temp=root->right; TreeNode *ptr=temp; while(ptr->left!=nullptr) { ptr=ptr->left; } root->val=ptr->val; root->right=deleteNode(root->right,ptr->val); } } else if(key<root->val) { root->left=deleteNode(root->left,key); } else { root->right=deleteNode(root->right,key); } return root; } };
669. 修剪二叉搜索树 ------看不懂,只看了答案
链接:代码随想录
108. 将有序数组转换为二叉搜索树
链接:力扣
/** * 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: //二分? TreeNode* sortedArrayToBST(vector<int>& nums) { TreeNode *temp=build(nums, 0,nums.size()-1); return temp; } TreeNode *build(vector<int>& nums,int low,int high) { if(low<=high) { int mid=low+(high-low)/2; TreeNode *root=new TreeNode(nums[mid]); root->left=build(nums,low,mid-1); root->right=build(nums,mid+1,high); return root; } return nullptr; } };
538. 把二叉搜索树转换为累加树labuladong 题解思路
链接:力扣
/** * 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: int sum=0; TreeNode* convertBST(TreeNode* root) { helper(root); return root; } void helper(TreeNode *root) { if(root!=nullptr) { helper(root->right); sum+=root->val; root->val=sum; helper(root->left); } } };