The sword refers to the offer face question 18 tree's substructure (recursion), the face question 192 fork Tree's Mirror (recursive and non-recursive stack) __ The sword refers to the offer

Source: Internet
Author: User
face Test 18: substructure of the Tree (recursive)

Determine whether the binary tree B is a subtree of binary tree A.

Train of thought: Two steps: 1, recursive call Hassubtree first traverse a The value of the node in a and B's root node is the same, if there is, call Doestree1havetree2 to do the second step of judgment. 2, determine whether AB structure is the same, that is, the recursive judgment of the left and right nodes.

Java implementation:

public class Doestree1havetree2 {private Boolean dosetree1havetree2 (Bitree A,bitree b) {if (B = = null) return true;//note
		If order if (A = = null) return false;
		if (A.value!= b.value) return false;
	Return Dosetree1havetree2 (A.left, B.left) && dosetree1havetree2 (A.right, b.right);
		Private Boolean Hassubtree (Bitree A,bitree B) {Boolean result = false;
			if (A!= null && B!= null) {if (A.value = = b.value) {result = Dosetree1havetree2 (a,b);
			} if (!result) {result = Hassubtree (A.left, B);
			} if (!result) {result = Hassubtree (A.right, B);
	} return result;
		public static void Main (string[] args) {doestree1havetree2 test = new Doestree1havetree2 ();
		Bitree A1 = new Bitree (0);
		Bitree A2 = new Bitree (1);
		Bitree A3 = new Bitree (2);
		Bitree A4 = new Bitree (3);
		Bitree A5 = new Bitree (4);
		A1.left = A2; A1.
		right = A3;
		A2.left = A4; A2.
		right = A5;
		Bitree B1 = new Bitree (1);
		Bitree B2 = new Bitree (3); Bitree B3 = new BitreE (4);
		B1.left = B2; B1.
		right = B3;
	System.out.println (Test.hassubtree (A1, B1));
	class bitree{int value;
	Bitree left;
	Bitree right;
	Bitree (int x) {value = x;
 }
}
face Test 19: Two fork Tree mirroring (recursive and non-recursive)

Recursive and non-recursive Java implementations:

public class Mirrorrecursively {
	//recursive, first swap the left and right nodes of the node, and then recursively call the left and right nodes.
	static void mirrorrecursively (Bitree tree) {
		if (tree!= null) {
			Bitree temp = tree.left;
			Tree.left = tree. Right;
			Tree. right = temp;
			if (tree.left!= null) mirrorrecursively (tree.left);
			if (tree. Right!= null) mirrorrecursively (tree. right);
		}
	Non-recursive, with stacks. Or to exchange the left and right nodes, and then put the root node into the stack, take the Zoozi node as a new root node, until the left node is empty, then the root node out of the stack, take the right-hand node as a new root node, loop.
	static Bitree Zhan (bitree) {
		Bitree p = tree;
		stack<bitree> stack = new stack<> ();
		while (P!= null | | |!stack.isempty ()) {//node is empty, and stack is null-terminated while
			(P!= null) {
				Bitree temp = p.left;
				P.left = p.right;
				P.right = temp;
				Stack.push (p);
				P=p.left;
			}
			p = Stack.pop ();
			p = p.right;
		}
		return tree;
	}

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.