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. 浅谈JavaScript中的定时器

    引言 使用setTimeout()和setInterval()创建的定时器可以实现很多有意思的功能.很多人认为定时器是一个单独的线程(之前我也是),但是JavaScript是运行在单线程环境中的,而定 ...

  2. Java并发编程核心方法与框架-CompletionService的使用

    接口CompletionService的功能是以异步的方式一边生产新的任务,一边处理已完成任务的结果,这样可以将执行任务与处理任务分离.使用submit()执行任务,使用take取得已完成的任务,并按 ...

  3. mysql和oracle的区别(功能性能、选择、使用它们时的sql等对比)

    一.并发性 并发性是oltp数据库最重要的特性,但并发涉及到资源的获取.共享与锁定. mysql:mysql以表级锁为主,对资源锁定的粒度很大,如果一个session对一个表加锁时间过长,会让其他se ...

  4. 网页JQ基础之jq-隐藏以及显示特效

    简单的 隐藏以及显示的 JQ 的代码如下: <!DOCTYPE html> <html> <head> <script src="/jquery/j ...

  5. git 初始化

    Git global setup git config --global user.name "杨清1" git config --global user.email " ...

  6. ThikPHP3.1 常用方法(one)

    公司常用但没学过的一些函数,记录一下备份. 1,在Rest操作方法中,可以使用$this->_type获取当前访问的资源类型,用$this->_method获取当前的请求类型. 2.uns ...

  7. eclipse svn快捷键

    一.打开eclipse插件安装市场,搜索svn,选择Subclipse安装 二.设置 svn ,设置快捷键, 1.windows-preference,在打开对话框输入keys过滤出keys选择 2. ...

  8. LR常用函数整理

    1,变量转参数lr_save_string("aaa","param"):将字符串"aaa"或者一个字符串变量,转变成LR的参数{param ...

  9. iOS resign code with App Store profile and post to AppStore

    http://stackoverflow.com/questions/17545452/ios-resign-code-with-app-store-profile-and-post-to-appst ...

  10. css 设计总结

    一.背景图片的拉伸: backgroud-size 说明:  http://www.w3school.com.cn/cssref/pr_background-size.asp 效果:  http:// ...