Binary Tree和Binary Search Tree

Binary Tree
Definition: at most two children node.

Binary Tree Example:

                10 ==root

               /      \

              13          15  cur

             /   \     /  \

            21  72      12    2

            /  \

          null null

class TreeNode{

  int value;

  TreeNode * left;

  TreeNode * right;

  TreeNode * parent //point to this node's parent node.

}
Common interview questions:

Basic knowledge points 1: tree traverse

1. pre-order.

2.in-order.

3.post-order.

Key point: base case is usually empty leaf nodes below the node.

 

Balanced binary tree:

For any node of the tree, the height difference between the left and right sub-tree of subtrees does not exceed 1 .

 

Complete binary tree (complete binary tree)

The bottom layer is an array, the array internal number must be continuous, not a spare memory space.

 

Binary Search Tree (binary search tree)

          10

         /      \

        5        15

       /   \       /     \

      2     7      12     20

Note: For (all the nodes in the right sub-tree) root 10, must the entire left subtree (all the nodes in the left subtree) must be less than 10, the entire right subtree must be greater than 10.

Meanwhile binary search tree does not allow duplicate node;

Binary tree is often the most common and most closely combined with recursion type of interview questions.

the reason:

1. each node included in the nature, and the value of the next layer transmission properties tend consistent, relatively easy to define recursive rule.

2.base case: null pointer under the leaf node.

3.Example1:int getHeight(Node root)

4.Example2: there are a number of statistics tree node.

 

Common Interview Questions:

How to get integer value(height) for a problem with size = n? But how?

GetHeight of a binary tree?

public int getHeight(TreeNode root){

  if(root == null){

  return 0;

}  

  int left = getHeight(root.left);

  int right = getHeight(root.right);

  return 1 + Math.max(left,right);

 

}

Time = (n);

space = O(n) == O(height);

 

---------------------------------------------------------------------------------------------------------------------------

Q3:How to determine whether a binary tree is a balanced binary tree?

public boolean isBalanced(TreeNode tree){

    if(root == null){

      return true;

}  

    int left = getHeight(root.left);

    int right = getHeight(root.right);

    if(Math.abs(left - right) > 1){

      return false;

  }  

     return  isBalanced(root.left) && isBalanced(root.right);

}

Time complexity analysis:

            isBalanced(n/2 + n/2)

             /      \

          getHeight getHeight

          (n/4 + n/4)  (n/4 + n/4)

Since it is a balanced binary tree: Number of floors logn So: Time: O (nlogn)

---------------------------------------------------------------------------------------------------------------------------

Q4: how to determine a binary tree left and right sides are not symmetrical?

          10

         5a | 5b

       1a 3a | 3b 1b

    2a4a 6a8a   |    8b6b  4b2b

 public boolean isSymmetric(TreeNode noe,TreeNode two){

    if(one == null && two == null){

      return true;

    }
    if(one ==null || two == null){

      return false;

}

    if(one.value == two.value){

      return false;

}

    return isSymmetric(one.left,two.right) && isSymmetric(one.right,one.left);

}

Time = O(n);

space = O(n) -> O(height)   if the tree is balaced -> O(logn)

---------------------------------------------------------------------------------------------------------------------------

Q5:

 

 

    

 

Guess you like

Origin www.cnblogs.com/xujiangxi/p/11223761.html