Interview preparation series 03 ---- Summary of Binary Tree questions in the interview, 03 ---- Binary Tree

Source: Internet
Author: User

Interview preparation series 03 ---- Summary of Binary Tree questions in the interview, 03 ---- Binary Tree

Interview preparation series 03 ---- Summary of Binary Tree questions in the interview

This article is the first article in the interview preparation series. The binary tree is the same as the linked list. First, we should think of recursion. Therefore, we recommend that you use recursion and non-recursion to complete each question. The previous article isInterview preparation series 03-Summary of Binary Tree questions in the interview

1. binary tree traversal, pre-order, post-order, recursive, and non-recursive

Http://blog.csdn.net/sheepmu/article/details/28941285

2. Sequence traversal of Binary Trees

3. Binary Tree Height

4. Number of nodes in a binary tree

5. Search for Binary Tree Images

6. Determine whether two binary trees are mirroring each other

7. Determine whether a tree is an image tree.

8. Determine whether two binary trees are the same.

9. Determine whether tree 1 is a sub-structure of tree 2.

10. Determine whether a binary tree is a balanced binary tree.

11. number of nodes in the k-layer of a binary tree

12. Number of leaf nodes in a binary tree

13. reconstruct a binary tree by means of forward and middle Traversal

14. reconstruct a binary tree by means of central and post-order traversal

15. Maximum Distance between two nodes in a binary tree

16. path of a binary tree and a specific value

17. Calculate the minimum common ancestor node of the two nodes in the binary tree.



 

Package com. sheepmu; import java. util. using list; import java. util. stack; class TreeNode {String value; TreeNode left; TreeNode right; public TreeNode (String value) {this. value = value ;}} public class BinaryTree {// 2. sequence traversal of Binary Trees // train of thought: Use queues to achieve sequence traversal of Binary Trees. Public void cx (TreeNode root) {if (root = null) return; queue list <TreeNode> queue = new queue list <TreeNode> (); queue. addLast (root); while (! Queue. isEmpty () {TreeNode cur = queue. removeFirst (); System. out. print (cur. value + ""); if (cur. left! = Null) queue. addLast (cur. left); if (cur. right! = Null) queue. addLast (cur. right);} // 3. height of a binary tree -- Recursion -- public int getHighRec (TreeNode root) {if (root = null) return 0; return Math. max (getHighRec (root. left), getHighRec (root. right) + 1;} // 3. height of a binary tree -- Non-Recursion -- // train of thought: sequence traversal, counting the number of nodes on the current and next layers. Public int getHigh (TreeNode root) {if (root = null) return 0; queue list <TreeNode> queue = new queue list <TreeNode> (); queue. addLast (root); int high = 0; int curLevelNodes = 1, nextLevelNodes = 0; while (! Queue. isEmpty () {TreeNode cur = queue. removeFirst (); curLevelNodes --; if (cur. left! = Null) {queue. addLast (cur. left); nextLevelNodes ++;} if (cur. right! = Null) {queue. addLast (cur. right); nextLevelNodes ++;} if (curLevelNodes = 0) {high ++; curLevelNodes = nextLevelNodes; nextLevelNodes = 0 ;}} return high ;}// 4. number of nodes in a binary tree -- Recursion -- public int getNodesNumRec (TreeNode root) {if (root = null) return 0; return getNodesNumRec (root. left) + getNodesNumRec (root. right) + 1;} // 4. number of nodes in a binary tree -- Recursion -- // thought: number of records in sequence traversal public int getNodesNum (TreeNode root) {if (root = null) return 0; Skip List <TreeNode> Queue = new queue list <TreeNode> (); queue. addLast (root); int num = 1; while (! Queue. isEmpty () {TreeNode cur = queue. removeFirst (); if (cur. left! = Null) {queue. addLast (cur. left); num ++;} if (cur. right! = Null) {queue. addLast (cur. right); num ++;} return num;} // 5. retrieve the image of a binary tree (directly change the original tree to its image tree, that is, destroy the original tree) -- Recursion -- // idea: Set the left subtree of the original tree to the image of its right subtree; set the right subtree of the original tree to the image public TreeNode getJXRec (TreeNode root) {if (root = null) return null; TreeNode tleft = getJXRec (root. right); TreeNode tright = getJXRec (root. left); root. left = tleft; root. right = tright; return root;} // 5. binary Tree image (directly turning the original Tree into its image Tree, that is, destroying the original Tree) -- Non-Recursion -- // idea: Using Stsck, let the subnodes of the node exchange public TreeNode getJX (Tree Node root) {if (root = null) return null; Stack <TreeNode> stack = new Stack <TreeNode> (); stack. push (root); while (! Stack. isEmpty () {TreeNode cur = stack. pop (); TreeNode temp = cur. right; cur. right = cur. left; cur. left = temp; if (cur. right! = Null) stack. push (cur. right); if (cur. left! = Null) stack. push (cur. left);} return root;} // 5. binary Tree image (generate a new tree without changing the original tree structure) -- Recursion -- public TreeNode newJXRec (TreeNode root) {if (root = null) return null; treeNode newTree = new TreeNode (root. value); newTree. left = newJXRec (root. right); newTree. right = newJXRec (root. left); return newTree;} // 5. binary Tree image (generate a new tree without changing the original tree structure) -- Non-Recursion --/* A/\ B C/\ D E F */public static void main (String [] args) {TreeNode n1 = new TreeNode ("A"); TreeNode n2 = new TreeNode ("B"); TreeNode n3 = new TreeNode ("C "); treeNode n4 = new TreeNode ("D"); TreeNode n5 = new TreeNode ("E"); TreeNode n6 = new TreeNode ("F"); n1.left = n2; n1.right = n3; n2.left = n4; n2.right = n5; n3.right = n6; TreeNode root = n1; BinaryTree bt = new BinaryTree (); System. out. print ("sequence traversal ---->"); bt. cx (root); System. out. print ("\ n"); System. out. println ("recursive height ---->" + bt. getHighRec (root); System. out. println ("non-recursive height ---->" + bt. getHighRec (root); System. out. println ("Number of recursive nodes ---->" + bt. getNodesNumRec (root); System. out. println ("Number of non-recursive nodes ---->" + bt. getNodesNum (root); // bt. getJXRec (root); // bt. cx (root); // System. out. print ("\ n"); System. out. print ("the post-sequence traversal of the image tree itself ---->"); bt. getJX (root); bt. cx (root );}}



Binary Tree interview questions

In-depth traversal. Each node is assigned the current maximum value.
 
How can I prepare for an interview with the Hong Kong University? I would like to ask some questions about the University of Hong Kong's interview questions over the years.

For full English interview, it is recommended that you prepare for the interview, for example, go to a famous training class or a New Oriental oral class. As far as I know, 80% of our interviewees had been prepared or trained for a long time, so they were dominant in English interviews.

In the first round of interviews, I usually had 5-7 people in a group. When I entered the interview, I introduced myself (from 30 s to 2 m ). After self-introduction, the professor will usually throw a question and give it to your group for free discussion. This may take 10 minutes and the professor will not participate. He thinks that the discussion will be terminated when the discussion is almost done. He may ask one of you to summarize the speeches made by all of you just now. At last, the professor may give you free time to ask questions that students want to know. The entire interview is about 20 m to half an hour.
If you have been controversial during the first round of interviews (the two professors scored a huge disparity, or one insisted on recording the other without recording the records), or you need to talk about scholarship when the scores are excellent, or your performance is very conservative, but your college entrance examination score is very high. If the professor wants to give you more opportunities, you will have the opportunity to participate in the second round of interviews.
The second round of interviews is generally one-to-one or multiple-to-one. In short, you have the opportunity to present them one by one. There is no fixed form or time for the second round of interviews. Everything depends on the interview with your professors and the purpose they ask you to take part in the second round.

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.