Print Common Nodes in Two Binary Search Trees
Given two Binary Search Trees, find common nodes in them. In other words, find intersection of two BSTs.
Example:
from: http://www.geeksforgeeks.org/print-common-nodes-in-two-binary-search-trees/
Method 1 (Simple Solution) A simple way is to one by once search every node of first tree in second tree. Time complexity of this solution is O(m * h) where m is number of nodes in first tree and h is height of second tree.
Method 2 (Linear Time) We can find common elements in O(n) time.
1) Do inorder traversal of first tree and store the traversal in an auxiliary array ar1[].
2) Do inorder traversal of second tree and store the traversal in an auxiliary array ar2[]
3) Find intersection of ar1[] and ar2[]. See this for details.
Time complexity of this method is O(m+n) where m and n are number of nodes in first and second tree respectively. This solution requires O(m+n) extra space.
Method 3 (Linear Time and limited Extra Space) We can find common elements in O(n) time and O(h1 + h2) extra space where h1 and h2 are heights of first and second BSTs respectively.
The idea is to use iterative inorder traversal. We use two auxiliary stacks for two BSTs. Since we need to find common elements, whenever we get same element, we print it.
public List<Integer> commonNodes(TreeNode root1, TreeNode root2) {
List<Integer> list = new ArrayList<Integer>(); Stack<TreeNode> stack1 = new Stack<TreeNode>();
Stack<TreeNode> stack2 = new Stack<TreeNode>(); while (root1 != null) {
stack1.push(root1);
root1 = root1.left;
} while (root2 != null) {
stack2.push(root2);
root2 = root2.left;
} while (stack1.size() > && stack2.size() > ) {
TreeNode node1 = stack1.peek();
TreeNode node2 = stack2.peek(); if (node1.val == node2.val) {
list.add(node1.val);
node1 = stack1.pop().right;
node2 = stack2.pop().right; while (node1 != null) {
stack1.push(node1);
node1 = node1.left;
} while (node2 != null) {
stack2.push(node2);
node2 = node2.left;
}
} else if (node1.val < node2.val) {
node1 = stack1.pop().right; while (node1 != null) {
stack1.push(node1);
node1 = node1.left;
}
} else {
node2 = stack2.pop().right;
while (node2 != null) {
stack2.push(node2);
node2 = node2.left;
}
}
}
return list;
}
Print Common Nodes in Two Binary Search Trees的更多相关文章
- [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 ...
- Method for balancing binary search trees
Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...
- [LeetCode] 235. 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 ...
- Optimal binary search trees
问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...
- LEETCODE —— Unique Binary Search Trees [动态规划]
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- LeetCode-96. Unique Binary Search Trees
Description: Given n, how many structurally unique BST's (binary search trees) that store values 1.. ...
- Unique Binary Search Trees I & II
Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
随机推荐
- NoSQL 简介及什么是AICD
NoSQL 简介 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL". 在现代的计算系统上每天网络上都会产生庞大的数据量. 这些数据有很大一部分是由关 ...
- jQuery 元素的选中, 置顶、上移、下移、置底、删除
如截图: <ul> <li class="li01" onclick="C_columnSetTop(this)"><i>& ...
- Ajax load html page
jQuery ajax - load() 方法 jQuery Ajax 参考手册 实例 使用 AJAX 请求来改变 div 元素的文本: $("button").click(fun ...
- Java并发编程核心方法与框架-CyclicBarrier的使用
CyclicBarrier类似于CountDownLatch也是个计数器,不同的是CyclicBarrier数的是调用了CyclicBarrier.await()进入等待的线程数,当线程数达到了Cyc ...
- Mysql分表和分区的区别、分库分表介绍与区别
分表和分区的区别: 一,什么是mysql分表,分区 什么是分表,从表面意思上看呢,就是把一张表分成N多个小表,具体请看:mysql分表的3种方法 什么是分区,分区呢就是把一张表的数据分成N多个区块,这 ...
- [转]asp.net webform 与mvc 共享session
公司内部系统最早是用.net webform模式开发的,现新项目用.net mvc 开发,现存在的问题就是如何保持原有.net webform的登录状态不变,而在mvc中能够验证用户的登录状态,也就是 ...
- 推荐近期15个 Node.js 开发工具
近来Node.js 越来月流行了,这个基于Google V8 引擎建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.在本文中,我们列出了2015年最佳的15个 Node.js 开发工具.这些 ...
- H5移动端知识点总结
H5移动端知识点总结 阅读目录 移动开发基本知识点 calc基本用法 box-sizing的理解及使用 理解display:box的布局 理解flex布局 Flex布局兼容知识点总结 回到顶部 移动开 ...
- document.all的详细解释(document.all基本上所有浏览器可用!)
从何而来从IE4开始IE的object model才增加了document.all对象,MSDN中也对 Object.all 有详细的说明,Object.all是个HTMLCollection,不是数 ...
- solr多条件查询(一)
每个项目的数据结构可能不同,查询的格式有可能不同,本项目所有的字段为动态的,所以整理了一下, 1.查询所有字段中包含”测试“但是所有的TM不包含”江苏大学“ q:X_1457955996315KEY: ...