Java二叉树常见题型

基本操作

前序遍历

题目链接

class Solution {
    List<Integer> list=new ArrayList<>();
    public List<Integer> preorderTraversal(TreeNode root) {
        if(root==null)return list;
        list.add(root.val);
        preorderTraversal(root.left);
        preorderTraversal(root.right);
        return list;
    }
}

非递归形式

class Solution {
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                list.add(cur.val);
                stack.push(cur);
                cur=cur.left;
            }
            cur=stack.pop();
            cur=cur.right;
        }
        return list;

    }
}

中序遍历

class Solution {
    List<Integer> list=new ArrayList<>();
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null)return list;
        inorderTraversal(root.left);
        list.add(root.val);
        inorderTraversal(root.right);
        return list;
    }
}

非递归版本

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        //TreeNode cur=root;
        while(root!=null||!stack.isEmpty()){
            while(root!=null){
                stack.push(root);
                root=root.left;
            }
            root=stack.pop();
            list.add(root.val);
            root=root.right;
        }
        return list;
    }
}

后序遍历

class Solution {
    List<Integer> list=new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        if(root==null)return list;
        postorderTraversal(root.left);
        postorderTraversal(root.right);
        list.add(root.val);
        return list;
    }
}

非递归形式

class Solution {
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        if(root==null)return list;
        Stack<TreeNode> stack=new Stack<>();
        TreeNode prev=null;
        while (root!=null||!stack.isEmpty()) {
            while (root!=null) {
                stack.push(root);
                root=root.left;
            }
            root=stack.pop();
            //prev记录已经打印过的节点
            if (root.right==null||root.right==prev) {
                list.add(root.val);
                prev=root;
                root=null;
            } else {
                stack.push(root);
                root=root.right;
            }
        }
        return list;
    }
}

求节点个数

    public int getSize2(BTnode root){
        if(root==null)return 0;
        return getSize2(root.left)+getSize2(root.right)+1;
    }

求叶子节点个数

    public int getLeafSize2(BTnode root){
        if(root==null)return 0;
        if(root.left==null&&root.right==null){
            return 1;
        }
        return getLeafSize2(root.left)+getLeafSize2(root.right);
    }

求第K层节点个数

     public int getKLevelSize(BTnode root,int K){
        if(root==null)return 0;
        //递归过程中,K为1说明找到了这一层
        if(K==1){
            return 1;
        }
     return getKLevelSize(root.left,K-1)+getKLevelSize(root.right,K-1);

层序遍历

题目链接

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res=new ArrayList<>();
        if(root==null)return res;

        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            List<Integer> list=new ArrayList<>();
            //每一行的长度
            int len=queue.size();
            for(int i=1;i<=len;i++){
                TreeNode cur=queue.poll();
                list.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if(cur.right!=null){
                   queue.offer(cur.right);
                }
            }
            res.add(list);
        }
        return res;
    }
}

平衡二叉树

题目链接

class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null)return true;
        return Math.abs(getHeight(root.left)-getHeight(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right);
    }
    //求高度函数
    public int getHeight(TreeNode root){
        if(root==null)return 0;
        int left=getHeight(root.left);
        int right=getHeight(root.right);
        return left>right?left+1:right+1;
    }
}

合并二叉树

题目链接

class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        //特殊情况
        if(root1==null)return root2;
        if(root2==null)return root1;
        //前中后遍历任选一个
        //两树合并放在root1上
        root1.left=mergeTrees(root1.left,root2.left);
        root1.val=root1.val+root2.val;
        root1.right=mergeTrees(root1.right,root2.right);
        return root1;
    }
}

对称二叉树

题目链接

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null)return false;
        return check(root,root);
    }
    public boolean check(TreeNode p,TreeNode q){
        if(p==null&&q==null)return true;
        if(p==null||q==null)return false;
        if(p.val!=q.val)return false;
        return check(p.left,q.right)&&check(p.right,q.left);
    }
}

翻转二叉树

题目链接

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null)return root;
        TreeNode left=invertTree(root.left);
        TreeNode right=invertTree(root.right);
        root.left=right;
        root.right=left;
        return root;
    }
}

二叉树的镜像

题目链接

class Solution {
    public TreeNode mirrorTree(TreeNode root) {
        if(root==null)return root;
        if(root.left==null&&root.right==null)return root;
        TreeNode cur=root.left;
        root.left=root.right;
        root.right=cur;
        mirrorTree(root.left);
        mirrorTree(root.right);
        return root;
    }
}

二叉树的深度

题目链接

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)return 0;
        int left=maxDepth(root.left);
        int right=maxDepth(root.right);
        return left>right?left+1:right+1;
    }
}

二叉树的最近公共祖先

题目链接

思路:

三种情况:
1.p 和 qq 在 rootroot 的子树中,且分列 rootroot 的 异侧(即分别在左、右子树中);
2.p = rootp=root ,且 qq 在 rootroot 的左或右子树中;
3.q = rootq=root ,且 pp 在 rootroot 的左或右子树中;

根据 left 和 right ,可展开为四种情况;
1.当 left 和 right 同时为空 :说明 root 的左 / 右子树中都不包含 p,q ,返回 null ;
2.当 left 和 right 同时不为空 :说明 p,q 分列在 root 的 异侧 (分别在 左 / 右子树),因此 root 为
近公共祖先,返回 root ;
3.当 left 为空 ,right 不为空 :p,q 都不在 root 的左子树中,直接返回right 。具体可分为两种情况:
①p,q 其中一个在root 的 右子树 中,此时 right 指向p(假设为p );
②p,q 两节点都在 root 的 右子树 中,此时的 right 指向 最近公共祖先节点 ;
4.当 left 不为空 , right 为空 :与情况 3. 同理;

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root==null)return null;
        if(p==root||q==root)return root;
        TreeNode left=lowestCommonAncestor(root.left,p,q);
        TreeNode right=lowestCommonAncestor(root.right,p,q);
        if(left==null)return right;
        if(right==null)return left;
        return root;
    }
}

另一个树的子树

题目链接

class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(t==null)return true;
        if(s==null)return false;
        return isSubtree(s.left,t)||isSubtree(s.right,t)||isSameTree(s,t);
    }
    //相同的树
    public boolean isSameTree(TreeNode p,TreeNode q){
        if(p==null&&q==null)return true;
        if(p==null||q==null)return false;
        if(p.val!=q.val)return false;
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}

相同的树

题目链接

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null&&q==null)return true;
        if(p==null||q==null)return false;
        if(p.val!=q.val)return false;
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}

递增顺序搜索树

题目链接

class Solution {
    public TreeNode increasingBST(TreeNode root) {
        List<Integer> list=new ArrayList<>();
        order(root,list);
        TreeNode ans=new TreeNode(0);
        TreeNode cur=ans;
        for(int num:list){
            cur.right=new TreeNode(num);
            cur=cur.right;
        }
        return ans.right;
    }
    //中序遍历
    public void order(TreeNode root,List<Integer> list){
        if(root==null)return;
        order(root.left,list);
        list.add(root.val);
        order(root.right,list);
    }
}

😒

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
(1)非递归定义 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除结点外n0 , 其余的每一个结点都有且仅有一个直接前驱结点;有零个或多个直接后继结点。 (2)递归定义 一颗大树分成几个大的分枝,每个大分枝再分成几个小分枝,小分枝再分成更小的分枝,… ,每个分枝也都是一颗树,由此我们可以给出树的递归定义。 树(tree)是由n(n≥0)个结点组成的有限集合。n=0的树称为空树;n>0的树T: ① 有且仅有一个结点n0,它没有前驱结点,只有后继结点。n0称作树的根(root)结点。 ② 除根结点之外的其他结点分为m(m≥0)个互不相交的集合T0,T1,…,Tm-1,其中每个集合Ti(0≤i<m)本身又是一棵树,称为根的子树(subtree)。 2、掌握树的各种术语: (1) 父母、孩子与兄弟结点 (2) 度 (3) 结点层次、树的高度 (4) 边、路径 (5) 无序树、有序树 (6) 森林 3、二叉树的定义 二叉树(binary tree)是由n(n≥0)个结点组成的有限集合,此集合或者为空,或者由一个根结点加上两棵分别称为左、右子树的,互不相交的二叉树组成。 二叉树可以为空集,因此根可以有空的左子树或者右子树,亦或者左、右子树皆为空。 4、掌握二叉树的五个性质 5、二叉树的二叉链表存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值