Microsoft Interview: In binary tree, find two nodes' nearest common father
2014-08-02 15:57
435 查看
题目
二叉树中,找到任意两个节点最近的公共父节点。(微软onsite第三轮面试题)
思路
1 看到二叉树,总是从递归的方式开始想。
2 大多数题型,都是从深度遍历/广度遍历 转换而来,这道题目也不例外。
3 一次遍历完成不了?那么试着分开遍历。只要记录一个数到根节点的路径,再对记录进行操作就可以完成了。
4 用什么来记录?stack还是queue,根据代码的逻辑来。
5 如果是二叉查找树,可以简化方法。
代码
public static TreeNode findCommonFatherNode(TreeNode root, int i, int j) {
LinkedList<TreeNode> stack1 =new LinkedList<>();
LinkedList<TreeNode> stack2 =new LinkedList<>();
recordPath(root,i,stack1);
recordPath(root,j,stack2);
TreeNode answer = root;
while(!stack1.isEmpty()&&!stack2.isEmpty()){
TreeNode cur1 = stack1.pop();
TreeNode cur2 = stack2.pop();
if(cur1 != cur2){
break;
}
answer = cur1;
}
return answer;
}
private static boolean recordPath(TreeNode root,int val,LinkedList<TreeNode> stack){
if(root == null ){
return false;
}
if(root.val == val){
stack.push(root);
return true;
}
if(recordPath(root.left,val,stack)){
stack.push(root);
return true;
}
else if(recordPath(root.right,val,stack)){
stack.push(root);
return true;
}
return false;
}
二叉树中,找到任意两个节点最近的公共父节点。(微软onsite第三轮面试题)
思路
1 看到二叉树,总是从递归的方式开始想。
2 大多数题型,都是从深度遍历/广度遍历 转换而来,这道题目也不例外。
3 一次遍历完成不了?那么试着分开遍历。只要记录一个数到根节点的路径,再对记录进行操作就可以完成了。
4 用什么来记录?stack还是queue,根据代码的逻辑来。
5 如果是二叉查找树,可以简化方法。
代码
public static TreeNode findCommonFatherNode(TreeNode root, int i, int j) {
LinkedList<TreeNode> stack1 =new LinkedList<>();
LinkedList<TreeNode> stack2 =new LinkedList<>();
recordPath(root,i,stack1);
recordPath(root,j,stack2);
TreeNode answer = root;
while(!stack1.isEmpty()&&!stack2.isEmpty()){
TreeNode cur1 = stack1.pop();
TreeNode cur2 = stack2.pop();
if(cur1 != cur2){
break;
}
answer = cur1;
}
return answer;
}
private static boolean recordPath(TreeNode root,int val,LinkedList<TreeNode> stack){
if(root == null ){
return false;
}
if(root.val == val){
stack.push(root);
return true;
}
if(recordPath(root.left,val,stack)){
stack.push(root);
return true;
}
else if(recordPath(root.right,val,stack)){
stack.push(root);
return true;
}
return false;
}
相关文章推荐
- Find the nearest common ancestor of any two nodes in a binary search tree
- Find the common ancestor of two nodes in a binary tree
- Lowest Common Ancestor of Two Nodes in a Binary Tree
- **Lowest Common Ancestor of Two Nodes in a Binary Tree
- Lowest Common Ancestor of The Two Nodes in a Binary Tree
- find distance between two nodes in a binary tree
- Print Common Nodes in Two Binary Search Trees
- [CTCI] 4.6 First Common Ancestor for Two Nodes On a Binary tree
- ch4.6: find the LCA of 2 nodes in a binary tree with 3 different methods
- 501. Find Mode in Binary Search Tree
- LeetCode Find Mode in Binary Search Tree
- Lowest common ancestor in binary search tree
- [LeetCode] Find Mode in Binary Search Tree 找二分搜索数的众数
- LeetCode - 501 - Find Mode in Binary Search Tree
- Groupon面经:Find paths in a binary tree summing to a target value
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- 501. Find Mode in Binary Search Tree
- Leetcode 501 Find Mode in Binary Search Tree