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的更多相关文章

  1. [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 ...

  2. Method for balancing binary search trees

    Method for balancing a binary search tree. A computer implemented method for balancing a binary sear ...

  3. [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 ...

  4. Optimal binary search trees

    问题 该问题的实际应用 Suppose that we are designing a program to translate text from English to French. For ea ...

  5. LEETCODE —— Unique Binary Search Trees [动态规划]

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  6. LeetCode-96. Unique Binary Search Trees

    Description: Given n, how many structurally unique BST's (binary search trees) that store values 1.. ...

  7. Unique Binary Search Trees I & II

    Given n, how many structurally unique BSTs (binary search trees) that store values 1...n? Example Gi ...

  8. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  9. 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 ...

随机推荐

  1. [webgrid] – selecterow - (Get Selected Row from ASP.NET MVC 3 WebGrid)

    Get Selected Row from ASP.NET MVC 3 WebGrid Abstract: The following article demonstrates how to get ...

  2. android-android获取navigationview 上的控件id

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); View headerView = navi ...

  3. fedora23忘记root密码怎么办??

    fedora23使用的是uefi, 不是 传统的grub 所以在编辑grub的时候, 跟以前的版本略有不同 最最重要的是: 在编辑启动条目的时候, 那个 linuxefi ... vmlinuz... ...

  4. Highcharts属性中英文参照

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

  5. webpack构建与loaders

    loaders 定义 先了解一下webpack,webpack是一个用于针对js文件的构建工具,在被构建的js文件中,我们可以使用require语句和webpack loader,如下: var cs ...

  6. javascript简单的认识下return语句+2015的总结+2016的展望

    好久没更新博客了...自从有了mac之后世界变得简单了...日常么,除了研究代码,看别人的代码,写自己的代码.就那样.... 吐槽点:window配个nodejs的环境花了九头牛两只老虎的力气,mac ...

  7. R语言 recommenderlab 包

    recommend li_volleyball 2016年3月20日 library(recommenderlab) ## Warning: package 'recommenderlab' was ...

  8. 安装 vue.js和第一个hello world

    一.在自己的项目文件中使用npm下载vue npm install vue 二.在文件中引入vue.js 三.第一个hello world 注:scritpt代码必须写在html代码的下面

  9. Swift3.0P1 语法指南——字符串与字符

    原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...

  10. Unity内存申请和释放

    转自:http://www.jianshu.com/p/b37ee8cea04c 1.资源类型 GameObject, Transform, Mesh, Texture, Material, Shad ...