树的基本概念
1.节点的度:一个节点含有的子树的个数称为该节点的度。
2.树的度:一棵树中,最大的节点的度称为树的度。
3.叶子节点或终端节点:度为0的节点称为叶节点;
4.双亲节点或父节点:若一个节点含有子节点,则这个节点称为其子节点的父节点;
5.孩子节点或子节点:一个节点含有的子树的根节点称为该节点的子节点;
6.根结点:一棵树中,没有双亲结点的结点;
7.节点的层次:从根开始定义起,根为第1层,根的子节点为第2层,以此类推;
8.树的高度或深度:树中节点的最大层次;
9.非终端节点或分支节点:度不为0的节点;
10.兄弟节点:具有相同父节点的节点互称为兄弟节点;
11.堂兄弟节点:双亲在同一层的节点互为堂兄弟;
12.节点的祖先:从根到该节点所经分支上的所有节点;
13.子孙:以某节点为根的子树中任一节点都称为该节点的子孙。
14.森林:由m(m>=0)棵互不相交的树的集合称为森林
树与非树的区别
二叉树
二叉树的特点
一棵二叉树是结点的一个有限集合,该集合或者为空,或者是由一个根节点加上两棵别称为左子树和右子树的二叉树组成。
- 每个结点最多有两棵子树,即二叉树不存在度大于 2 的结点。
- 二叉树的子树有左右之分,其子树的次序不能颠倒。
两种特殊的二叉树
- 完全二叉树: 完全二叉树是效率很高的数据结构,完全二叉树是由满二叉树而引出来的。对于深度为K的,有n个结点的二叉树,当且仅当其每一个结点都与深度为K的满二叉树中编号从1至n的结点一一对应时称之为完全二叉树。满二叉树是一种特殊的完全二叉树。
- 满二叉树: 一个二叉树,如果每一个层的结点数都达到最大值,则这个二叉树就是满二叉树。也就是说,如果一个二叉树的层数为K,且结点总数是(2^k) -1 ,则它就是满二叉树。
二叉树深度优先遍历(前中后)
递归
private static class Node{
private char val;
private Node left=null;
private Node right=null;
private Node(char val){this.val=val;}
public String toString(){
return String.format("{%c}",val);
}
}
//前序遍历
/**
* 前序遍历 根-左子树-右子树
**/
public static void preOrderTraversal(Node root){
if(root==null)
return;
System.out.println(root.val);
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
//中序遍历(左子树-根-右子树)
public static void inOrderTraversal(Node root) {
if(root==null)
return;
inOrderTraversal(root.left);
System.out.println(root.val);
inOrderTraversal(root.right);
}
//后序遍历(左子树-右子树-根)
public static void postOrderTraversal(Node root){
if(root==null)
return ;
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.println(root.val);
}
求结点个数
//结点个数
public static int count=0;
public static void getSize(Node root){
if(root==null)
return;
count++;
getSize(root.left);
getSize(root.right);
}
//汇总思想(左子树结点个数+右子树结点个数+根)
public static int getSize2(Node root){
if(root==null)
return 0;
int a=getSize2(root.left);
int b=getSize2(root.right);
return a+b+1;
}
中序遍历放到list中
private static List<Character>list=new ArrayList<>();
private static void inorderReturnList(Node root) {
if(root!=null){
inorderReturnList(root.left);
list.add(root.val);
inorderReturnList(root.right);
}
}
private static List<Character> inorderReturnList2(Node root){
List<Character>list=new ArrayList<>();
if(root==null)
return list;
list.addAll(inorderReturnList2(root.left));
list.add(root.val);
list.addAll(inorderReturnList2(root.right));
return list;
}
求叶子结点个数
private static int leftsize=0;
private static void getLeafSize(Node root) {
if(root==null)
return;
if(root.left==null&&root.right==null)
leftsize++;
getLeafSize(root.left);
getLeafSize(root.right);
}
//左子树叶子结点+右子树叶子结点
private static int getLeafSize2(Node root) {
if(root==null) return 0;
if(root.left==null&&root.right==null) return 1;
int a=getLeafSize2(root.left);
int b=getLeafSize2(root.right);
return a+b;
}
//求树的高度(左右子树高度最大值加1)
public static int getHeight(Node root) {
if(root==null)
return 0;
int a=getHeight(root.left);
int b=getHeight(root.right);
return Math.max(a,b)+1;
}
//求第k层结点个数
public static int getKLevel(Node root, int k){
if(root==null)
return 0;
if(k==1)
return 1;
return getKLevel(root.left,k-1)+getKLevel(root.right,k-1);
}
//查找val所在的结点
public static Node find(Node root, int val) {
if(root==null)
return null;
if(root.val==val)
return root;
Node n=find(root.left,val);
if(n!=null){
return n;
}
return find(root.right,val);
}
public static boolean find2(Node root, int val){
if(root==null) return false;
if(root.val==val) return true;
if(find2(root.left,val))
return true;
return find2(root.right,val);
}
//检查两棵树是否相同
//根相同左子树相同右子树相同
public boolean isSameTree(TreeNode p, TreeNode q) {
if(p==null&&q==null)return true;
if(p==null||q==null)return false;
return p.val==q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
//另一棵树的子树
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null)return false;
if(Same(s,t))
return true;
if(isSubtree(s.left,t))
return true;
return isSubtree(s.right,t);
}
public static boolean Same(TreeNode p,TreeNode q){
if(p==null&&q==null)return true;
if(p==null||q==null)return false;
return p.val==q.val&&Same(p.left,q.left)&&Same(p.right,q.right);
}
判断一棵二叉树是否为平衡二叉树
public boolean isBalanced(TreeNode root) {
if(root==null)
return true;
if(!isBalanced(root.left))
return false;
if(!isBalanced(root.right))
return false;
int a=getHight(root.left);
int b=getHight(root.right);
if((a-b)>=-1&&(a-b)<=1)
return true;
return false;
}
public static int getHight(TreeNode root){
if(root==null)return 0;
return Math.max(getHight(root.left),getHight(root.right))+1;
}
非递归
深度遍历搜索,非递归用栈。
//二叉树非递归前序遍历
public List<Integer> preorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) return list;
TreeNode node = root;
while (!stack.isEmpty() || node != null) {
while (node != null) {
list.add(node.val);
stack.push(node);
node = node.left;
}
TreeNode top = stack.pop();
node = top.right;
}
return list;
}
//非递归中序遍历
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) return list;
TreeNode node = root;
while (!stack.isEmpty() || node != null) {
while (node != null) {
stack.push(node);
node = node.left;
}
TreeNode top = stack.pop();
list.add(top.val);
node = top.right;
}
return list;
}
//非递归后序遍历
public List<Integer> postorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
Stack<TreeNode> stack = new Stack<>();
if (root == null) return list;
TreeNode node = root;
TreeNode pre = null;//上次被遍历完的结点
while (!stack.isEmpty() || node != null) {
while (node != null) {
stack.push(node);
node = node.left;
}
TreeNode top = stack.peek();
if (top.right == null || top.right == pre) {
list.add(top.val);
stack.pop();
pre = top;
} else {
node = top.right;
}
}
return list;
}
二叉树的广度优先遍历(分层遍历)
分层遍历用队列,先把根入队列,遍历队列,出队首元素,把根的值入list,如果根的左子树不为空,把左子树入队列,如果根的右子树不为空,把根的右子树入队列。
public List<List<Integer>> levelOrder2(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null) return list;
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int lev=0;
while(!queue.isEmpty()){
list.add(new ArrayList<>());
int len=queue.size();
for(int i=0;i<len;i++){
TreeNode node=queue.remove();
list.get(lev).add(node.val);
if(node.left!=null)
queue.add(node.left);
if(node.right!=null)
queue.add(node.right);
}
lev++;
}
return list;
}
递归:找递推公式+出口。
二叉树练习题都用汇总的思想:根+左子树+右子树。