zoukankan      html  css  js  c++  java
  • 二叉树的各个考点

    1.判断a树是不是b树的子树

    package 树;
    
    import java.util.ArrayList;
    
    import com.java.util.TreeNode;
    
    /**
     * @author wangpei
     * @version 创建时间:2017年5月21日 下午2:47:33 判断a树是不是b树的子树
     */
    public class 判断a树是不是b树的子树 {
        static ArrayList<TreeNode> array = new ArrayList<TreeNode>();
    
        public static void main(String[] args) {
            TreeNode r1 = new TreeNode(1);
            r1.left = new TreeNode(2);
            r1.right = new TreeNode(3);
            r1.left.left = new TreeNode(4);
            r1.left.right = new TreeNode(5);
            r1.left.left.left = new TreeNode(8);
            r1.left.left.right = new TreeNode(9);
            r1.right.left = new TreeNode(6);
            r1.right.right = new TreeNode(7);
            TreeNode r2 = new TreeNode(2);
            r2.left = new TreeNode(4);
            r2.right = new TreeNode(5);
            // findRoot(r1,r2);
            findRoot(r1, r2);
            System.out.println(IsZijiegou(array.get(0), r2));
    
        }
    
        public static boolean IsZijiegou(TreeNode r1, TreeNode r2) {// 判断r2是不是r1的子结构
    
            if (r1 == null && r2 == null) {
                return true;
            } else if (r1 == null || r2 == null) {
                return false;
            }
            if (r1 != null && r2 != null) {
                if (r1.value != r2.value) {
                    return false;
                } else {
                    return IsZijiegou(r1.left, r2.left)
                            && IsZijiegou(r1.right, r2.right);
                }
            }
            return false;
    
        }
    
        public static void findRoot(TreeNode r1, TreeNode r2) {// 找到r2的根节点在r1中的位置
            if (r1 != null) {
                if (r2.value == r1.value) {
    
                    array.add(r1);
    
                }
                findRoot(r1.left, r2);
                findRoot(r1.right, r2);
            }
    
        }
    }
    

    2.二叉树的层次遍历

    package 树;
    
    import java.util.LinkedList;
    import java.util.Queue;
    import com.java.util.TreeNode;
    /** 
     * @author wangpei 
     * @version 创建时间:2017年5月25日 下午10:01:30 
     * 类说明 
     */
    public class 二叉树的层次遍历2 {
    
    
        public static void main(String[] args) {
    
            TreeNode root=new TreeNode(1);
            root.left=new TreeNode(2);
            root.left.left=new TreeNode(4);
            root.left.right=new TreeNode(5);
            root.right=new TreeNode(3);
            root.right.left=new TreeNode(6);
            root.right.right=new TreeNode(7);
            cengciPrint(root);
    
    
        }
        public static void cengciPrint(TreeNode root){
            Queue<TreeNode> pq=new LinkedList<TreeNode>();
            pq.add(root);
            while(pq.size()!=0){//队列中有值
                int len=pq.size();
                for(int i=0;i<len;i++){
                    TreeNode t=pq.poll();
                    System.out.print(t.value+" ");
                    if(t.left!=null)
                    pq.add(t.left);
                    if(t.right!=null)
                    pq.add(t.right);
    
                }
    
            }
    
        }
    
    }
    

    3.递归交换左右子树

    package 树;
    
    import com.java.util.TreeNode;
    
    /**
     * @author wangpei
     * @version 创建时间:2017年5月25日 下午9:54:51 类说明
     */
    public class 交换左右子树 {
        public static void main(String[] args) {
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.left.left = new TreeNode(4);
            root.left.right = new TreeNode(5);
            root.right = new TreeNode(3);
            root.right.left = new TreeNode(6);
            root.right.right = new TreeNode(7);
            change(root);
    
            print(root);
            // System.out.println(root.right.value+"]]]");
        }
    
        public static void change(TreeNode root) {
            TreeNode temp;
            if (root != null) {
                temp = root.left;
                root.left = root.right;
                root.right = temp;
                change(root.left);
                change(root.right);
            }
    
        }
    
        public static void print(TreeNode root) {
            if (root != null) {
                System.out.print(root.value + " ");
                print(root.left);
                print(root.right);
    
            }
    
        }
    
    }
    

    4.求二叉树的深度
    解释:二叉树的深度深度:二叉树中根到叶子节点最长的路径。
    代码:通过递归来做 求得二叉树中左右子树的最长路径,再向上遍历。

    package 树;
    
    import com.java.util.TreeNode;
    
    /** 
     * @author wangpei 
     * @version 创建时间:2017年8月3日 下午5:52:54 
     * 类说明 
     */
    public class 子树test {
        public static void main(String[] args) {
            TreeNode root = new TreeNode(2);
            root.left = new TreeNode(3);
            root.left.left = new TreeNode(3);
            root.left.right = new TreeNode(4);
            System.out.println(getLength(root));
        }
        public static int getLength(TreeNode root){
            if(root!=null){
                return getLength(root.left)>getLength(root.right)?getLength(root.left)+1:getLength(root.right)+1;
            }
            return 0;
        }
    
    
    
    
    }
    

    5.统计二叉树中的叶子节点
    分析:
    递归遍历,根节点为空,返回null,叶子节点,返回1,都不满足,返回左右子树的叶子节点和

    package 树;
    
    import com.java.util.TreeNode;
    
    /**
     * @author wangpei
     * @version 创建时间:2017年8月3日 下午6:00:56 类说明
     */
    public class 统计叶子节点数目 {
        public static int count = 0;
    
        public static void main(String[] args) {
            TreeNode root = new TreeNode(1);
            root.left = new TreeNode(2);
            root.right = new TreeNode(3);
            root.left.left = new TreeNode(4);
            root.left.right = new TreeNode(5);
    
            System.out.println(getNumber(root));
        }
    
        public static int getNumber(TreeNode root) {
            int n1, n2;
            if (root == null)
                return 0;
            if (root.left == null && root.right == null)
                return 1;
            n1 = getNumber(root.left);
            n2 = getNumber(root.right);
            return n1 + n2;
    
        }
    
    }
    

    6.二叉搜索树的后续遍历序列
    7.二叉树中和为某一值的路径
    8.二叉树的镜像
    9.树的最低公共祖先
    10.两个节点的最低公共祖先
    11.对称的二叉树

  • 相关阅读:
    【CF1029A】Many Equal Substrings(模拟)
    【CF1028C】Rectangles(线段树)
    【CF1028B】Unnatural Conditions(构造)
    【CF1028A】Find Square(签到)
    【CF1025C】Plasticine zebra(模拟)
    【CF1025A】Doggo Recoloring(签到)
    167.数据传送指令
    166.寻址方式
    165.基础
    164.多媒体操作系统
  • 原文地址:https://www.cnblogs.com/wangxiaopei/p/8551204.html
Copyright © 2011-2022 走看看