【leetcode 2】Binary Tree

本文探讨了二叉树的基本概念,包括定义(如平衡二叉树和二叉搜索树),遍历方法(如前序、中序和后序),以及如何判断树的平衡性和结构相似性。同时介绍了在工业界的应用,如社交网络分析和信息索引压缩。着重讲解了递归在解决这些问题中的关键作用和典型例子,如在给定范围内打印BST的键值。
摘要由CSDN通过智能技术生成

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值