Nearest common ancestor of two nodes found in binary tree __ recursion

Source: Internet
Author: User
Tags stringbuffer


Import java.util.*;

		To find the nearest common ancestor public class getlowesttree{//Two fork tree node in a binary tree two nodes are defined as the publicly static class node{public int value;

		Public Node left;

		public Node right;
		public Node (int data) {this.value=data; }///Find the nearest public ancestor of two nodes Getlowestancestor (node Head,node n1,node n2) {//Recursive exit if (head==null| | n1==head| |
		N2==head) {return head;  ///Recursive call Node left=getlowestancestor (HEAD.LEFT,N1,N2); Zoozi Tree Node right=getlowestancestor (HEAD.RIGHT,N1,N2);
		Right subtree if (left!=null&&right!=null) {return head;

	return left!=null?left:right; //***************************************************//visual print binary tree public static void Printtree (Node head) {System
		. OUT.PRINTLN ("Binary tree:");
		Printinorder (head, 0, "H", 17);
	System.out.println ();
		public static void Printinorder (Node head, int height, String to, int len) {if (head = = null) {return;
		Printinorder (head.right, height + 1, "V", Len); String VAl = to + Head.value + to;
		int lenm = Val.length ();
		int lenl = (LEN-LENM)/2;
		int lenr = LEN-LENM-LENL;
		val = Getspace (lenl) + val + getspace (lenr);
		System.out.println (getspace (height * len) + val);
	Printinorder (head.left, height + 1, "^", Len);
		public static string getspace (int num) {String space = ' ";
		StringBuffer buf = new StringBuffer ("");
		for (int i = 0; i < Num. i++) {buf.append (space);
	return buf.tostring (); //***************************************************//Advanced Problem-method one (construct hash table) public static class Record1 {private H

		Ashmap<node, node> map;
			The public Record1 (Node head) {map = new Hashmap<node, node> ();
			if (head!= null) {map.put (head, NULL);
		} setmap (head);
			The private void Setmap (Node head) {if (head = = null) {return;
			} if (Head.left!= null) {Map.put (head.left, head);
			} if (head.right!= null) {Map.put (head.right, head);
			} setmap (Head.left); Setmap (Head.right);
			Public node query (node O1, node O2) {hashset<node> path = new hashset<node> ();
				while (Map.containskey (O1)) {Path.add (O1);
			O1 = Map.get (O1);
			while (!path.contains (O2)) {O2 = Map.get (O2);
		return O2;

		}//Advanced problem--Method two public static class Record2 {private Hashmap<node, hashmap<node, node>> map;
			Public Record2 (Node head) {map = new Hashmap<node, Hashmap<node, node>> ();
			Initmap (head);
		Setmap (head);
			The private void Initmap (Node head) {if (head = = null) {return;
			Map.put (head, New Hashmap<node, node> ());
			Initmap (Head.left);
		Initmap (Head.right);
			The private void Setmap (Node head) {if (head = = null) {return;
			} Headrecord (Head.left, head);
			Headrecord (Head.right, head);
			Subrecord (head);
			Setmap (Head.left);
		Setmap (Head.right);
			private void Headrecord (node n, node h) {if (n = = null) {return; } map.get (n).Put (H, h);
			Headrecord (N.left, h);
		Headrecord (N.right, h);
			The private void Subrecord (Node head) {if (head = = null) {return;
			} preleft (Head.left, Head.right, head);
			Subrecord (Head.left);
		Subrecord (Head.right);
			private void Preleft (node L, node R, node h) {if (L = = null) {return;
			} preright (L, R, h);
			Preleft (L.left, R, h);
		Preleft (L.right, R, h);
			private void Preright (node L, node R, node h) {if (r = = null) {return;
			Map.get (L). Put (R, h);
			Preright (L, R.left, H);
		Preright (L, R.right, H);
			Public node query (node O1, node O2) {if (O1 = = O2) {return o1;
			} if (Map.containskey (O1)) {return Map.get (O1). Get (O2);
			} if (Map.containskey (O2)) {return Map.get (O2). get (O1);
		return null;
		} public static void Main (String[]args) {//system.out.println ("Hello");
		Node Head=new node (1);
		Head.left=new Node (2);
		Head.right=new Node (3);
Head.left.left=new Node (4);		Head.left.right=new Node (5);
		Head.right.left=new Node (6);
		Head.right.right=new Node (7);
    
        Head.right.right.left=new Node (8);
        Printtree (head);
		Node Node=getlowestancestor (head,head.left,head.right);


        System.out.println (Node.value);
		Node O1 = head.left.right;
		Node O2 = Head.right.left;
		It is convenient to query multiple times after generating map--advanced problem Record1 record1 = new Record1 (head);

		Record2 record2 = new Record2 (head);
		System.out.println ("O1:" + o1.value);
		System.out.println ("O2:" + o2.value);
		System.out.println ("ancestor:" + record1.query (O1, O2). Value);

		System.out.println ("ancestor:" + record2.query (O1, O2). Value);
		O1 = Head.left.left;
		O2 = Head.left;
		System.out.println ("O1:" + o1.value);
		System.out.println ("O2:" + o2.value);
		System.out.println ("ancestor:" + record1.query (O1, O2). Value);

		System.out.println ("ancestor:" + record2.query (O1, O2). Value);
		O1 = Head.right.left;
		O2 = Head.right.right.left; System.out.println ("O1:" + o1.value);
		System.out.println ("O2:" + o2.value);
		System.out.println ("ancestor:" + record1.query (O1, O2). Value);

	System.out.println ("ancestor:" + record2.query (O1, O2). Value); }
		
}


Related Article

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.