二叉树相关代码

该文介绍了二叉树的基本操作,包括如何构建二叉树,以及使用递归和非递归方法进行前序、中序、后序遍历。此外,还涉及层次遍历、计算树的高度和叶子数,以及判断两棵树是否等价、反转二叉树和二叉树的深拷贝问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二叉树相关的知识

1、建树
public TreeNode create(String[] c){
        String var = c[size++];
        if(var == null || size > c.length){
            return null;
        }
        TreeNode node = new TreeNode(var);
        node.left = create(c);
        node.right = create(c);
        return node;
    }
2、前序遍历(递归&非递归)
	public void preOrder1(TreeNode root){//递归
        if (root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrder1(root.left);
        preOrder1(root.right);
    }
    public void preOrder2(TreeNode root){
        if (root == null){
            return;
        }
        TreeNode current;
        LinkedList<TreeNode> s = new LinkedList<>();
        s.addFirst(root);
        while(!s.isEmpty()){
            current = s.removeFirst();
            System.out.print(current.val + " ");
            if (current.right != null){
                s.addFirst(current.right);
            }
            if (current.left != null){
                s.addFirst(current.left);
            }
        }
    }
3、中序遍历(递归&非递归)
	public void inOrder1(TreeNode root){//递归
        if (root == null){
            return;
        }
        inOrder1(root.left);
        System.out.print(root.val + " ");
        inOrder1(root.right);
    }
    public void inOrder2(TreeNode root){
        TreeNode current = root;
        LinkedList<TreeNode> s = new LinkedList<TreeNode>();
        while(!s.isEmpty() || current != null){
            while (current != null){
                s.addFirst(current);
                current = current.left;
            }
            if (!s.isEmpty()){
                current = s.removeFirst();
                System.out.print(current.val + " ");
                current = current.right;
            }
        }
    }
4、后序遍历(递归&非递归)
	public void postOrder1(TreeNode root){//递归
        if (root == null){
            return;
        }
        postOrder1(root.left);
        postOrder1(root.right);
        System.out.print(root.val + " ");
    }
    public void postOrder2(TreeNode root){
        TreeNode current = root;
        LinkedList<TreeNode> s1 = new LinkedList<>();
        LinkedList<TreeNode> s2 = new LinkedList<>();
        while(!s1.isEmpty() || current != null){
            while(current != null){
                s1.addFirst(current);
                s2.addFirst(current);
                current = current.right;
            }
            if(!s1.isEmpty()){
                current = s1.removeFirst();
                current = current.left;
            }
        }
        while (!s2.isEmpty()){
            System.out.print(s2.removeFirst().val + " ");
        }
    }
5、层次遍历
	public void level(TreeNode root){
        if (root == null){
            return;
        }
        LinkedList<TreeNode> q = new LinkedList<>();  //模拟队列
        q.addFirst(root);
        TreeNode current;
        while (!q.isEmpty()){
            current = q.removeLast();
            System.out.print(current.val + " ");
            if (current.left != null){
                q.addFirst(current.left);
            }
            if(current.right != null){
                q.addFirst(current.right);
            }
        }
    }
6、树的高度&叶子数
	public int countHigh(TreeNode root){
        if(root == null)
            return 0;
        else{
            int lhight = countHigh(root.left);
            int rhight = countHigh(root.right);
            return Math.max(lhight,rhight)+1;
        }
    }
    public int countNum(TreeNode root){
        if(root == null)
            return 0;
        if(root.left == null && root.right == null){
            return 1;
        }
        return countNum(root.right)+countNum(root.left);
    }
6、等价树 & 反转二叉树 & 二叉树拷贝
	//等价树
    public boolean isEqual(TreeNode p, TreeNode q){
        if(p == null && q == null){
            return true;
        }else if(p == null || q == null){
            return false;
        }else if(!p.val.equals(q.val)){
            return false;
        }else {
            return isEqual(p.left, q.left) && isEqual(p.right, q.right);
        }
    }
    //反转二叉树
    public void invert(TreeNode root){
        if(root == null){
            return;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        invert(root.left);
        invert(root.right);
    }
    //二叉树拷贝
    public TreeNode cloneTree(TreeNode root){
        TreeNode node = null;
        if(root == null){
            return null;
        }
        node = new TreeNode(root.val);
        node.left = cloneTree(root.left);
        node.right = cloneTree(root.right);
        return node;
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值