代码随想录算法训练营第十七天 | 654.最大二叉树, 617.合并二叉树,700.二叉搜索树中的搜索 & 98.验证二叉搜索树
654. Maximum Binary Tree
思路:这道题是直接看的视频,讲得也太好了(第n次感叹)
题目链接/文章讲解:文章讲解
视频讲解:视频讲解
我的代码:
C++:
/**
* 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) {
if (nums.size() == 1) {
TreeNode* node = new TreeNode(nums[0]);
return node;
}
int maxNum = INT_MIN;
int maxIdx;
for (int i=0; i<nums.size(); i++) {
if (nums[i] > maxNum) {
maxNum = nums[i];
maxIdx = i;
}
}
TreeNode* root = new TreeNode(maxNum);
if (maxIdx > 0) {
vector<int> leftNums(nums.begin(), nums.begin()+maxIdx);
root->left = constructMaximumBinaryTree(leftNums);
}
if (maxIdx < nums.size()-1) {
vector<int> rightNums(nums.begin()+maxIdx+1, nums.end());
root->right = constructMaximumBinaryTree(rightNums);
}
return root;
}
};
617. Merge Two Binary Trees
思路:这一道题还是直接看了视频然后写的,写得还挺顺利的
题目链接/文章讲解:文章讲解
视频讲解:视频讲解
我的代码:
555难得有心情c++和python都写了一遍,赶进度omggg
Python:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def mergeTrees(self, root1, root2):
"""
:type root1: TreeNode
:type root2: TreeNode
:rtype: TreeNode
"""
if not root1:
return root2
if not root2:
return root1
root1.val += root2.val
root1.left = self.mergeTrees(root1.left, root2.left)
root1.right = self.mergeTrees(root1.right, root2.right)
return root1
C++:
/**
* 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) {
return root2;
}
if (!root2) {
return root1;
}
root1->val += root2->val;
root1->left = mergeTrees(root1->left, root2->left);
root1->right = mergeTrees(root1->right, root2->right);
return root1;
}
};
700. Search in a Binary Search Tree
思路:这道题秒了哈哈哈哈,我的intuision是迭代法,感觉对于写递归还是有些心理上的障碍,明明有些不是很难,但个人就是更喜欢迭代法,感觉每一步都很清晰
题目链接/文章讲解: 搜索二叉树文章讲解
视频讲解:搜索二叉树视频讲解
我的代码:
Python:
递归法
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
if not root or root.val == val:
return root
if val < root.val:
return self.searchBST(root.left, val)
if val > root.val:
return self.searchBST(root.right, val)
迭代法
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""
ptr = root
while ptr:
if val < ptr.val:
ptr = ptr.left
elif val > ptr.val:
ptr = ptr.right
else:
return ptr
return None
98. Validate Binary Search Tree
思路:之前有刷过的好像,所以印象很深可以利用中序单调递增来验证二叉搜索树,但是自己写的话只能写出先create一个列表的版本,直接用全局变量能想到一点但是写不好。卡哥真的每一步的迭代都预判了我内心的想法哈哈哈哈哈
题目链接:98. Validate Binary Search Tree
题目链接/文章讲解:验证二叉搜索树文章讲解
视频讲解:验证二叉搜索树视频讲解
我的代码:
Python:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.cur = None
def isValidBST(self, root: Optional[TreeNode]) -> bool:
if not root:
return True
left = self.isValidBST(root.left)
if self.cur != None and root.val >= self.cur.val:
self.cur = root
else:
return False
right = self.isValidBST(root.right)
return left and right
总结
救命 三周的进度要赶
最近刷二叉树的题逐渐让我对写recursion脱敏了,是个好兆头