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的更多相关文章

  1. 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 ...

  2. Binary Search Tree 以及一道 LeetCode 题目

    一道LeetCode题目 今天刷一道LeetCode的题目,要求是这样的: Given a binary search tree and the lowest and highest boundari ...

  3. 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 ...

  4. [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 ...

  5. 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 ...

  6. 【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 ...

  7. PAT甲级——1099 Build A Binary Search Tree (二叉搜索树)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Searc ...

  8. 【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 ...

  9. Binary search tree system and method

    A binary search tree is provided for efficiently organizing values for a set of items, even when val ...

随机推荐

  1. 两个 DataTable 读取重复数据,dataTable1与dataTable2不同

    protected void Page_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Ad ...

  2. SpringBoot_04springDataJPA

    说明:底层使用Hibernate 一.springDataJPA和mybatisPlus的使用区别 第一步: 把mybatisPlus的依赖.配置删除 包括:实体类的注解.引导类的mapperScan ...

  3. Python RSA操作

    公钥加密.私钥解密 # -*- coding: utf-8 -*- import rsa # rsa加密 def rsaEncrypt(str): # 生成公钥.私钥 (pubkey, privkey ...

  4. SSM框架返回json数据

    常见错误:No converter found for return value of type: class .................. 原因分析:这是因为springmvc默认是没有对象 ...

  5. 为什么你不看好家教O2O

    伴随着科技的不断进步,大众创业的激情在不断的燃烧着,于是又很多的朋友会往家教这条路上走.就有了家教O2O的出现,很多的人都在抢占市场,可是,为什么你不看好家教O2O? 家教O2O虽然能够帮助附近的朋友 ...

  6. IsDate(expression)函数

    IsDate 函数 返回 Boolean 值指明某表达式是否可以转换为日期. IsDate(expression) expression 参数可以是任意可被识别为日期和时间的日期表达式或字符串表达式. ...

  7. LoadPicture函数用法示例

    VB语言中LoadPicture函数用法示例: 本例使用 LoadPicture 函数将图片加载到窗体的 PictureBox 控件并从控件上清除掉该图片. 要试用此例,将 PictureBox 控件 ...

  8. web框架-(三)Django进阶

    通过上节课的学习,我们已经对Django有了简单的了解,现在来深入了解下~ 1. 路由系统 1.1 单一路由对应 url(r'^index$', views.index), 1.2 基于正则的路由 u ...

  9. do{}while(0);里面有continue

    do{}while(0);里面有continue,退出的只是do{}while(0);

  10. 每日一蠢 .kettle 下的kettle.properties文件内配置的内容不能被识别

    昨天装封装好的ETL 工具  窝将环境变量中的KETTLE_HOME删除了, 结果 .kettle 下的kettle.properties文件内配置的内容不能被识别 can't parse argum ...