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 ...
随机推荐
- dns泛解析漫谈
比如说:http://www.aaa.com/ 指向10.10.1.1,ftp.aaa.com/ 指向10.10.2.2,如果这时候客户访问的是aaa.com或者error.aaa.com (这里er ...
- struts.xml框架
1.首先在.jsp文件中<form action="/项目名称/login" method="post"> 2.然后浏览器会访问struts.xml ...
- ASP.NET MVC使用Bootstrap系统(2)——使用Bootstrap CSS和HTML元素
阅读目录 Bootstrap 栅格(Grid)系统 Bootstrap HTML元素 Bootstrap 验证样式 ASP.NET MVC创建包含Bootstrap样式编辑模板 小结 Bootstra ...
- 【转】apache kafka监控系列-KafkaOffsetMonitor
apache kafka监控系列-KafkaOffsetMonitor 时间 2014-05-27 18:15:01 CSDN博客 原文 http://blog.csdn.net/lizhitao ...
- vim ctags使用方法
一.用好系统自带软件ctags 大部分的unix系统都有ctags软件,它能跟vim很好地合作. 用途: 生成c语言的标签文件,实现相关c文件之间的跳转. 用法: 1.生成标签文件 ...
- 【CISP笔记】数据库及应用安全
数据库安全特性检查工具美国应用安全公司的App Detective英国下一代软件公司的NGS SQuirrel 常见应用安全威胁 网络层面拒绝服务.电子欺骗.嗅探.……系统层面Web服务漏洞.配置错误 ...
- C# 绘制统计图(柱状图, 折线图, 扇形图)【转载】
统计图形种类繁多, 有柱状图, 折线图, 扇形图等等, 而统计图形的绘制方法也有很多, 有Flash制作的统计图形, 有水晶报表生成统计图形, 有专门制图软件制作, 也有编程语言自己制作的:这里我们用 ...
- Javascript软键盘设计
国内大多数网站的密码在网络传输过程中都是明文的,我们目前正在做的产品也是这样的情形,这正常吗? 大家都偷懒?不重视安全?各人持有观点,有人认为明文传输并不是想象中的那么可怕,事实上正常情况下这些报文你 ...
- 2013nanjignB
B - Poor Warehouse Keeper Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & ...
- SQL补充
TOP 子句TOP 子句用于规定要返回的记录的数目.对于拥有数千条记录的大型表来说,TOP 子句是非常有用的.注释:并非所有的数据库系统都支持 TOP 子句.SELECT TOP 2 * FROM P ...