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 ...
随机推荐
- C/C++ 递归
递归 当一个函数调用它自己来定义时称它为递归函数.(什么叫它自己调用它自己呢?) 1.1.引出递归 从一个简单的问题考虑递归,求0,1,2, 3,4,5......n的和. 首先定义一个求和公式:su ...
- 关于websocket 在生产环境中遇到的问题 及 解决办法
一 生产环境说明 1) tornado 4.2 2) Tornado-MySQL 3) supervisor 3.0b2 4) protobuf 2.6.1 5) python 2.7.6 6) n ...
- 关于position的操作
1.position:relative 相较于正常位置的定位 <!DOCTYPE html> <html lang="en"> <head> & ...
- js工厂函数创建对象与对象构造函数的理解
工厂函数,顾名思义,就是通过一个"工厂的加工" 来创建一个对象的函数 //工厂函数 function createPerson(name,sex){ sex = sex == '男' ? '女' : ...
- Python 循环异或对文件进行加解密
# -* -coding: UTF-8 -* - # 功能:异或方式对文件进行加密和解密 import os import datetime # 主函数 def main(): getInput() ...
- oracle给用户赋dblink权限
create database link 别名(可任意起) connect to 需要连接库的用户名identified by 需要连接库的用户名 using '(DESCRIPTION =(ADDR ...
- 375-基于TI DSP TMS320C6657、XC7K325T的高速数据处理核心板
基于TI DSP TMS320C6657.XC7K325T的高速数据处理核心板 一.板卡概述 该DSP+FPGA高速信号采集处理板由我公司自主研发,包含一片TI DSP TMS320C6657和 ...
- 计蒜客 蓝桥模拟 A. 结果填空:矩阵求和
给你一个从 n×nn \times nn×n 的矩阵,里面填充 111 到 n×nn \times nn×n .例如当 nnn 等于 333 的时候,填充的矩阵如下. 1 1 2 3 2 4 5 ...
- Oracle RAC数据泵导出问题处理
1. 设置导出文件路径 sqlplus / as sysdba SQL> alter session set container=spdb1pdb; SQL> create directo ...
- 如何用 Jmeter 获取 Cookie
如何用 Jmeter 获取 Cookie 1.Jmeter 安装目录bin文件加下jmeter.properties文件修改,搜索CookieManager.save.cookies= 将Cookie ...