zoukankan      html  css  js  c++  java
  • 二叉树

    
    

    二叉树的概念

    完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的节点数都达到了最大个数,并且第h层的节点都连续的集中在最左边。想到点什么没?实际上,完全二叉树和堆联系比较紧密

    完全二叉树

    满二叉树:除最后一层外,每一层上的所有节点都有两个子节点,最后一层都是叶子节点。

    满二叉树

    哈夫曼树:给定n个权值作为n的叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman tree)。

    哈夫曼树

    二叉排序树:又称二叉查找树(Binary Search Tree),亦称二叉搜索树。二叉排序树或者是一棵空树,或者是具有下列性质的二叉树:

    • 若左子树不空,则左子树上所有结点的值均小于它的根结点的值;

    • 若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;

    • 左、右子树也分别为二叉排序树;

    • 没有键值相等的节点

    二分查找的时间复杂度是O(log(n)),最坏情况下的时间复杂度是O(n)(相当于顺序查找)

    二叉排序树

    平衡二叉树:又称 AVL 树。平衡二叉树是二叉搜索树的进化版,所谓平衡二叉树指的是,左右两个子树的高度差的绝对值不超过 1。

    红黑树:红黑树是每个节点都带颜色的树,节点颜色或是红色或是黑色,红黑树是一种查找树。红黑树有一个重要的性质,从根节点到叶子节点的最长的路径不多于最短的路径的长度的两倍。对于红黑树,插入,删除,查找的复杂度都是O(log N)。

    红黑树

    0.树

    package tree;

    public class TreeNode<T> {

       public TreeNode left;
       public TreeNode right;
       public T val;

       public TreeNode(T val) {
           this.val = val;
      }

       public TreeNode getLeft() {
           return left;
      }

       public void setLeft(TreeNode left) {
           this.left = left;
      }

       public TreeNode getRight() {
           return right;
      }

       public void setRight(TreeNode right) {
           this.right = right;
      }

       public T getVal() {
           return val;
      }

       public void setVal(T val) {
           this.val = val;
      }
    }

     

    1. 求二叉树中的节点个数

    递归解法: (1)如果二叉树为空,节点个数为0 (2)如果不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1

    public static int handler(TreeNode node){
           if (node == null){
               return 0;
          }
           return handler(node.left)+handler(node.right)+1;
      }

    2. 求二叉树的最大层数(最大深度)

    递归解法: (1)如果二叉树为空,二叉树的深度为0 (2)如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1

    public static int getMax(TreeNode node){
           if (node == null){
               return 0;
          }
           return Math.max(getMax(node.right), getMax(node.left))+1;
      }

    3.二叉树的最小深度

    /**
    * Definition for a binary tree node.
    * public class TreeNode {
    *     int val;
    *     TreeNode left;
    *     TreeNode right;
    *     TreeNode(int x) { val = x; }
    * }
    */
    class Solution {
       public int minDepth(TreeNode root) {
           if (root == null){
               return 0;
          }
           if (root.right == null&& root.left == null){
               return 1;
          }
           int min = Integer.MAX_VALUE;
           if (root.left != null){
               min = Math.min(minDepth(root.left), min);
          }
           if (root.right != null){
               min = Math.min(minDepth(root.right), min);
          }
           return min+1;
      }
    }

    4.后序遍历

     

     
  • 相关阅读:
    Bit Manipulation
    218. The Skyline Problem
    Template : Two Pointers & Hash -> String process
    239. Sliding Window Maximum
    159. Longest Substring with At Most Two Distinct Characters
    3. Longest Substring Without Repeating Characters
    137. Single Number II
    142. Linked List Cycle II
    41. First Missing Positive
    260. Single Number III
  • 原文地址:https://www.cnblogs.com/z-jx/p/11581646.html
Copyright © 2011-2022 走看看