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 ...
随机推荐
- AWK命令的用法
1.awk命令简介: awk是一种可以处理数据.产生格式化报表的语言,功能十分强大. awk的工作方式是读取数据,将每一行数据视为一条记录(record)每笔记录以字段分隔符分成若干字段,然后输出各个 ...
- yii2 funson86\yii2-setting
Yii2 Setting for other application, especially for Yii2 Adminlte Installation The preferred way to i ...
- Yii2 数据操作Query Builder(转)
Query Builder $rows = (new \yii\db\Query()) ->select(['dyn_id', 'dyn_name']) ->from('zs_dynast ...
- (转载)ecshop制作成手机网站的方法
ecshop用手机访问的时候,会自动跳转到 /mobile 目录下,ecshop自带的wap模板是用wml制作的,如果按这种情况,又需要制作一套模板,太麻烦,现在都是智能手机时代,wml模板已经不能 ...
- JQuery中==与===、$("#")与$("")的区别
首先,== equality 等同,=== identity 恒等.==, 两边值类型不同的时候,要先进行类型转换,再比较.===,不做类型转换,类型不同的一定不等. 下面分别说明:先说 ===,这个 ...
- [C#]System.Timers.Timer
摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...
- fedora 23如何实现 让root用户自动登录?
没想到很简单: 只是修改一个文件的一个地方: 修改: /etc/gdm/custom.conf文件, 将自动登录 启用为true, 然后自动登录的名字设为root 即可:
- PHP中spl_autoload_register()函数
spl_autoload_register — 注册给定的函数作为 __autoload 的实现 官方地址:http://php.net/manual/zh/function.spl-autoload ...
- GOF业务场景的设计模式-----单例模式
个人觉得 纯粹的学习设计模式,是不对的.也不能为了使用设计模式,而硬搬设计模式来使用 单例模式可能是 最简单的设计模式也是 大家知道最多的设计模式.当然 ,有很多种写法 定义:确保一个类只有一个实例, ...
- 有关jquery checkbox获取checked的问题
$("input").attr("checked"); <input type="checkbox" value="1&qu ...