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

Source: Internet
Author: User

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

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; returns list
 
  
Queue = new queue list
  
   
(); 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; returns list
   
    
Queue = new queue list
    
     
(); 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
     
      
Queue = new queue list
      
        (); 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, allow the child nodes of the node to exchange public TreeNode getJX (TreeNode root) {if (root = null) return null; Stack
       
         Stack = new Stack
        
          (); 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 );}}
        
       
      
     
    
   
  
 


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.