Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees
669.Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.
You may assume that the array is non-empty and the majority element always exist in the array.
Example 1:
Input:
1
/ \
0 2
L = 1
R = 2
Output:
1
\
2
Example 2:
Input:
3
/ \
0 4
\
2
/
1
L = 1
R = 3
Output:
3
/
2
/
1
Solution:
class Solution {
public:
TreeNode* trimBST(TreeNode* root, int L, int R) {
if (root == NULL) return NULL;
if (root->val < L) return trimBST(root->right, L, R);
if (root->val > R) return trimBST(root->left, L, R);
root->left = trimBST(root->left, L, R);
root->right = trimBST(root->right, L, R);
return root;
}
};
这道题我一开始考虑递归的时候返回条件考虑的是两个子树都为空的时候返回,写起来的思路就很混乱,多次提交都是RA。后来看了Discussion发现如果思路改为根节点为空时返回的话就可以减少很多不必要的情况讨论。说明对于递归仍然不太熟练,还是得学习一个。
617.Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.
You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.
Example 1:
Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/ \
4 5
/ \ \
5 4 7
做这道题时吸取了上一道题的经验。下面是我的代码——
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (t1 == NULL && t2 == NULL) return NULL;
TreeNode* t3 = new TreeNode(0);
if (t1 == NULL && t2 != NULL) {
(*t3).val = t2->val;
(*t3).left = mergeTrees(NULL, t2->left);
(*t3).right = mergeTrees(NULL, t2->right);
} else if (t2 == NULL && t1 != NULL) {
(*t3).val = t1->val;
(*t3).left = mergeTrees(t1->left, NULL);
(*t3).right = mergeTrees(t1->right, NULL);
} else {
(*t3).val = t1->val + t2->val;
(*t3).left = mergeTrees(t1->left, t2->left);
(*t3).right = mergeTrees(t1->right, t2->right);
}
return t3;
}
};
写完觉得代码冗余部分非常多,后来在Discussion看到这样的答案:
class Solution {
public:
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
if (!t1 && !t2) {
return nullptr;
}
TreeNode* node = new TreeNode((t1 ? t1->val : 0) + (t2 ? t2->val : 0));
node->left = mergeTrees((t1 ? t1->left : nullptr), (t2 ? t2->left : nullptr));
node->right = mergeTrees((t1 ? t1->right : nullptr), (t2 ? t2->right : nullptr));
return node;
}
};
写代码时多使用A ? B : C
的运算符可以有效减少代码的冗余,减少if else
结构的出现,使代码更简洁。
Week2 - 669. Trim a Binary Search Tree & 617. Merge Two Binary Trees的更多相关文章
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...
- Binary Search Tree 以及一道 LeetCode 题目
一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...
- PTA 04-树6 Complete Binary Search Tree (30分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/669 5-7 Complete Binary Search Tree (30分) A ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?
Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...
- 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
- PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...
- 【Lintcode】087.Remove Node in Binary Search Tree
题目: Given a root of Binary Search Tree with unique value for each node. Remove the node with given v ...
- Binary search tree system and method
A binary search tree is provided for efficiently organizing values for a set of items, even when val ...
随机推荐
- python基础知识之数据类型
一.与用户的交互 古时候,我们去银行取钱,需要有一个银行业务员等着我们把自己的账号密码输入给他, 然后他去进行验证,成功后,我们再将取款金额输入/告诉他 骄傲的现代人,会为客户提供一台ATM机(就是一 ...
- 04-A的LU分解
一.矩阵$AB$的逆 $(AB)^{-1}=B^{-1}A^{-1}$,顺序正好相反 二.$A=LU$ 如矩阵: $\left[\begin{array}{ll}{2} & {1} \\ {8 ...
- 初学Git——命令总结
首先,感谢廖雪峰老师制作的Git教程:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b0 ...
- jupyter安装
1. 安装ipython, jupyter pip install ipython pip install jupyter 2.查看是否有配置文件 jupyter notebook --generat ...
- SDOI2010代码拍卖会 (计数类DP)
P2481 SDOI2010代码拍卖会 $ solution: $ 这道题调了好久好久,久到都要放弃了.洛谷的第五个点是真的强,简简单单一个1,调了快4个小时! 这道题第一眼怎么都是数位DP,奈何数据 ...
- flask_session
flask_session和Flask中的session相比,比较简单,省去了 secret_key 首先,导入flask_session 模块 from flask_session import ...
- 使用AnnotationConfigApplicationContext注册配置类
1. AnnotationConfigApplicationContext功能 该类可以实现基于Java的配置类加载自定义在Spring的应用上下文的bean. 1.1 使用方式一:在构造方法中完成注 ...
- postman—Sandbox和断言
Postman沙盒 Postman Sandbox是一个JavaScript执行环境,您可以在编写预请求脚本和测试脚本(在Postman和Newman中)时可用.在这个沙箱中执行您在预请求/测试脚本部 ...
- JS中的执行机制(setTimeout、setInterval、promise、宏任务、微任务)
1.执行机制 JS 是单线程的,处理 JS 任务(程序)只能一个一个顺序执行,所以 JS 中就把任务分为了同步任务和异步任务.同步的进入主线程先执行,异步的进入Event Table并注册函数,当指定 ...
- echarts画环形图
alarmManage(){ let drawLine = echarts.init(document.getElementById('data-alarmManage-table-wrap')); ...