DAY22
654最大二叉树
自己做的时候忽略了:nums.length>=1的题给条件。所以每次递归都要判断是否size()>=1,不要空的。
- /**
- * 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) {
- TreeNode* node=new TreeNode(0);
- if(nums.size()==1) {
- node->val=nums[0];
- return node;
- }
- int index=0;
- for(int i=1;i<nums.size();i++)
- {
- if(nums[index]<nums[i]) index=i;
- }
- node->val=nums[index];
- if(index>0){
- vector<int> ln(nums.begin(),nums.begin()+index);
- node->left=constructMaximumBinaryTree(ln);
- }
- if(index<nums.size()-1){
- vector<int> rn(nums.begin()+index+1,nums.end());
- node->right=constructMaximumBinaryTree(rn);
- }
- return node;
- }
- };
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) return root2;
- if(root2==nullptr) return root1;
- root1->val+=root2->val;
- root1->left=mergeTrees(root1->left,root2->left);
- root1->right=mergeTrees(root1->right,root2->right);
- 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:
- TreeNode* searchBST(TreeNode* root, int val) {
- while(root!=nullptr)
- {
- if(root->val<val) root=root->right;
- else if(root->val>val) root=root->left;
- else return root;
- }
- return nullptr;
- }
- };
递归试试:
递归还是写不出来,早上看过答案也写不出来,逻辑理不清,什么时候该创建变量去接住递归的结果?
- /**
- * 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* searchBST(TreeNode* root, int val) {
- if(root==nullptr||root->val==val) return root;
- TreeNode* result=nullptr;
- if(root->val<val) {
- result=searchBST(root->right,val);
- }
- if(root->val>val){
- result=searchBST(root->left,val);
- }
- return result;
- }
- };
递归没接住(没找到),就返回的是初始化的result=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 {
- private:
- vector<int> result;
- void exc(TreeNode *root)
- {
- if(root==nullptr) return;
- exc(root->left);
- result.push_back(root->val);
- exc(root->right);
- }
- public:
- bool isValidBST(TreeNode* root) {
- result.clear();
- exc(root);
- for(int i=1;i<result.size();i++)
- {
- if(result[i-1]>=result[i]) return false;
- }
- return true;
- }
- };