Binary Tree recent common ancestor problem (O (n) time and only traverse once, O (1) Space (without considering the space of the function call stack), binary tree Space

Source: Internet
Author: User

Binary Tree recent common ancestor problem (O (n) time and only traverse once, O (1) Space (without considering the space of the function call stack), binary tree Space

Problem:

Find the closest common ancestor of the two nodes in the binary tree.

First of all, you can refer to the blog http://blog.csdn.net/cxllyg/article/details/7635992, write more detailed, including the node contains the parent pointer and does not include the parent pointer, also introduced the classic Tarjan algorithm.

The Tarjan algorithm is exquisite, but the extra O (n) storage space is required for using and querying sets. The third method in the above blog also needs to record the path from the root to the node, and requires O (log n) space. Of course, considering that we usually traverse the tree in a recursive way, therefore, the method call stack is the space usage of O (log n. However, this is for balanced binary trees. In the worst case, the space usage is O (n ).

Therefore, the algorithm I provided here does not need to record the path from the root node to the node, and only the tree can be traversed once.

1. First, traverse the tree in depth and find the first node, assuming p. Then, set the closest common ancestor of the two nodes to p.

2. Continue the in-depth traversal and find another node q. If q is found at this time, the closest ancestor of the two is p.

3. Otherwise, the node is rolled back to the upper layer, and the recent common ancestor of the two is changed to the parent node of p, because no other node q is found in the subtree rooted in p.

4. If so, continue to roll back to the previous layer if no data is found. When q is found, the corresponding public ancestor of the two is also found.

5. If p = q, p is directly returned as the closest common ancestor.

6. If neither of them exists in the tree, null is returned.


public class CommonAncestor {public static void main(String[] args) {CommonAncestor ca=new CommonAncestor();TreeNode root=new TreeNode(0);TreeNode l1=new TreeNode(-1);TreeNode r1=new TreeNode(1);root.left=l1;root.right=r1;TreeNode l1l1=new TreeNode(-2);TreeNode l1r1=new TreeNode(-3);l1.left=l1l1;l1.right=l1r1;TreeNode r=ca.commonAncestor(root, l1, r1);System.out.println(r.val);}private TreeNode ancestor=null;private TreeNode firstFound=null;private boolean found=false;public CommonAncestor(){}public TreeNode commonAncestor(TreeNode root,TreeNode p,TreeNode q){this.ancestor=null;this.found=false;findCommonAncestor(root,p,q);if(found)return ancestor;elsereturn null;}private void findCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {if(root==null)return ;if(found)return;this.findCommonAncestor(root.left, p, q);test(root,p,q);this.findCommonAncestor(root.right, p, q);test(root,p,q);}private void test(TreeNode root, TreeNode p, TreeNode q) {if(found)return;if(this.ancestor==null){if(root==p){this.ancestor=p;firstFound=p;if(p==q)found=true;}else if(root==q){this.ancestor=q;firstFound=q;if(p==q)found=true;}}else{if(root.left==this.ancestor||root.right==this.ancestor){this.ancestor=root;}if((root==p||root==q)&&root!=firstFound){found=true;}}}}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.