Leetcode:1305. 两棵二叉搜索树中的所有元素
Leetcode:1305. 两棵二叉搜索树中的所有元素
Leetcode:1305. 两棵二叉搜索树中的所有元素
思路
- BST树中序历遍有序。
- 利用双指针法可以在\(O(n)\)的复杂度内完成排序。
基于上述两个点,可以很简单的做出这道题。
- 先中序历遍得到两个有序的数列。
- 利用双指针法,只需历遍一次就可以得到升序的答案。
Talk is cheap . Show me the code .
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void inOrder(TreeNode *root,vector<int>& order){
        if(root==NULL) return ;
        inOrder(root->left,order);
        order.push_back(root->val);
        inOrder(root->right,order);
    }
    vector<int> getAllElements(TreeNode* root1, TreeNode* root2) {
        vector<int> a,b,ans;
        inOrder(root1,a),inOrder(root2,b);
        ans.resize(a.size()+b.size());
        int p=0,q=0,cnt=0;
        while(p<a.size()&&q<b.size()){
            if(a[p]<=b[q]) ans[cnt++]=a[p++];
            else if(a[p]>b[q]) ans[cnt++]=b[q++];
        }
        while(p<a.size()) ans[cnt++]=a[p++];
        while(q<b.size()) ans[cnt++]=b[q++];
        return ans;
    }
};
Leetcode:1305. 两棵二叉搜索树中的所有元素的更多相关文章
- LeetCode——1305. 两棵二叉搜索树中的所有元素
		给你 root1 和 root2 这两棵二叉搜索树. 请你返回一个列表,其中包含 两棵树 中的所有整数并按 升序 排序.. 示例 1: 输入:root1 = [2,1,4], root2 = [1,0 ... 
- [LeetCode] Delete Node in a BST 删除二叉搜索树中的节点
		Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ... 
- [LeetCode] 450. Delete Node in a BST 删除二叉搜索树中的节点
		Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ... 
- [LeetCode]230. 二叉搜索树中第K小的元素(BST)(中序遍历)、530. 二叉搜索树的最小绝对差(BST)(中序遍历)
		题目230. 二叉搜索树中第K小的元素 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 题解 中序遍历BST,得到有序序列,返回有序序列的k-1号元素. 代 ... 
- [LeetCode] Insert into a Binary Search Tree 二叉搜索树中插入结点
		Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert t ... 
- LeetCode:二叉搜索树中第K小的数【230】
		LeetCode:二叉搜索树中第K小的数[230] 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ... 
- LeetCode:二叉搜索树中的搜索【700】
		LeetCode:二叉搜索树中的搜索[700] 题目描述 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 N ... 
- Leetcode 701. 二叉搜索树中的插入操作
		题目链接 https://leetcode.com/problems/insert-into-a-binary-search-tree/description/ 题目描述 给定二叉搜索树(BST)的根 ... 
- LeetCode 230. 二叉搜索树中第K小的元素(Kth Smallest Element in a BST)
		230. 二叉搜索树中第K小的元素 230. Kth Smallest Element in a BST 题目描述 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的 ... 
随机推荐
- VRRP协议的原理与配置
			VRRP出现的原因: 局域网中的用户终端通常采用配置一个默认网关的形式访问外部网络,如果此时默认网关设备发生故障,将中断所有用户终端的网络访问,这很可能会给用户带来不可预计的损失. VRRP的优点: ... 
- 20个提高开发效率的JavaScript技巧
			减少代码行数和加快开发的技术! 我们在开发中,经常要写一些函数,如排序.搜索.寻找唯一的值.传递参数.交换值等,在这里我列出了我搜集的一些技术资源,可以像高手一样写出这些函数! JavaScript确 ... 
- 【NX二次开发】NX内部函数,libufunx.dll文件中的内部函数
			本文分为两部分:"带参数的函数"和 "带修饰的函数". 浏览这篇博客前请先阅读: [NX二次开发]NX内部函数,查找内部函数的方法 带参数的函数: void e ... 
- Django基础之auth模块
			内容概要 用户认证模块auth auth模块补充 auth_user表扩展字段 内容详细 auth模块 主要是用来做用户相关的功能 注册 登录 验证 修改密码 注销  访问admin需要管理员账号 ... 
- 04:CSS(02)
			溢出属性 p { height: 100px; width: 50px; border: 3px solid red; /*overflow: visible; !*默认就是可见 溢出还是展示*!*/ ... 
- ConcurrentSkipListSet - 秒懂
			疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 面试必备 + 面试必备 [博客园总入口 ] 疯狂创客圈 经典图书 : <Sprin ... 
- 关于Maven repository中pom.xml的jar包依赖
			https://mvnrepository.com 该mvn网站可以找到个个版本的依赖jar包 http://doc.canglaoshi.org 该网站为达内的开发文档服务器,可以找到很多开发中需 ... 
- csp-s模拟测试50(9.22)「施工(单调栈优化DP)」·「蔬菜(二维莫队???)」·「联盟(树上直径)」
			改了两天,终于将T1,T3毒瘤题改完了... T1 施工(单调栈优化DP) 考场上只想到了n*hmaxn*hmaxn的DP,用线段树优化一下变成n*hmaxn*log但显然不是正解 正解是很**的单调 ... 
- Arduino参考手册-函数和变量及电路图
			标题: Arduino参考手册-函数和变量及电路图 作者: 梦幻之心星 sky-seeker@qq.com 标签: [#Arduino,#参考手册,#函数,#变量] 目录: [Arduino] 日期: ... 
- react的三大属性
			react的三大属性 state props refs props 来自外部属性 states 来自内部状态 refs 用于表示组件内某个元素 state基础(最重要的属性) state是组件对象最 ... 
