【leetcode 2】Binary Tree

Binary Tree
Definition: at most two children node.

class TreeNode{
	int value;
	TreeNode* left;
	TreeNode* right;
}

工业界的应用:
Too many to list all;
e.g social networks analysis;
information indexing
information compression

基本知识点1:tree traverse

(1) pre-order
Implemenation with recursion 把cur放在最前面来打印
(2) in-order 把cur node 放在最中间
(3) post-order 把cur node 放在最后打印\

Trick: base case usually refers to the null childnode below the leaf node.

基本概念:

  • Balanced binary tree : is commonly defined as a binary tree in which the depth of the left and right subtree of every node differ 1 or less

  • Complete binary tree : is a binary tree in which every level, except possibly the last, is completed filled, and all nodes are as far left as possible

  • Binary Search Tree: for every single node in the tree, the values in its left subtree are all smaller than its vaule, and the values in its right subtree are all larger than its value.

  • Binary tree 是最常见的,和recursion结合最紧密的面试题目类型

  • Reasons
    1、 每层node具备的性质,传递的值和下一层的性质往往一致,比较容易定义 recursive rule
    2、Base case(generally): null pointer under the leaf node
    3、Example, int getHeight(Node *root)
    4、Example, 统计tree 里面有多少个node?

Fundamental Knowledge

  • Traversal of a binary tree
  • Definitation: Balanced binary tree、Complete binary tree、 Binary Search tree

1.1 how to determine whether a binary tree is a balanced binary tree?

1.2 How to judge whether a binary tree is sysmmetric?

1.3 Let’s assume if we tweak the lchild with rchild of an binary node in a binary node, then the “structrue” of the tree are not changed. Then how can ew deermine whether two binary trees’ structures are identical.

case1. Blanced
case2. un-balanced

1.3 how to determine whether a binary tree is a Binary Search tree

1.4 经典: print BST keys in the given range

Given two values k1 and k2(where k1 < k2) and a root pointer to a Binary Search Tree.
Print all the keys of tree in range k1 to k2.ie. Print all such that k1 <=x <=k2 and x is a key of given BST.
Print all the keys in an increasing order.

Property : For BST, if we print out all elements in an in-order sequence, then they satisfy that thay are printed in an increasing order.

你可能感兴趣的:(笔试面试相关,leetcode,算法)