牛客网常见面试题之二叉树操作

目录

1. 前序遍历32

2. 层次遍历40

3. 之字打印44

4.e二叉树的最大深度46

5.判断路径和55

6. 改造树为双向链表16

7.判断是对称二叉树21

8.合并树25

9.判断是否是镜像树28

10.判断是否是搜索二叉树36

11.判断是否是平衡二叉树47

12.二叉搜索树的公共父节点54

13.二叉树的公共节点04

14.重建二叉树22

15.输出右视图 29

16.判断是否是完全二叉树 41

总结:


1. 前序遍历32
ArrayList<Integer> res=new ArrayList<>();
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        trace(root);
        return res.stream().mapToInt(i->i).toArray();
    }

    private void trace(TreeNode root) {
        // TODO
        if(root==null) return;
        res.add(root.val);
        trace((root.left));
        trace(root.right);
    }
2. 层次遍历40
public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        // write code here
        
        ArrayList<ArrayList<Integer>> res=new ArrayList<>();
        if(root==null) return res;
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        while(!q.isEmpty()){
            int k=q.size();
            ArrayList<Integer> temp=new ArrayList<>();
            for(int i=0;i<k;i++){
                TreeNode t=q.poll();
                temp.add(t.val);
                if(t.left!=null) q.offer(t.left);
                if(t.right!=null) q.offer(t.right);
            }
            res.add(temp);
        }
        return res;

    }
3. 之字打印44
 public ArrayList<ArrayList<Integer>> Print (TreeNode root) {
        // write code here
    
        
        ArrayList<ArrayList<Integer>> res=new ArrayList<>();
        if(root==null) return res;
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        int flag=1;
        while(!q.isEmpty()){
            int k=q.size();
            LinkedList<Integer> temp=new LinkedList<>();
            for(int i=0;i<k;i++){
                TreeNode t=q.poll();
                if(flag==1)
                    temp.addLast(t.val);
                else
                    temp.addFirst(t.val);
                if(t.left!=null) q.offer(t.left);
                if(t.right!=null) q.offer(t.right);
            }
            res.add(new ArrayList(temp));
            flag=1-flag;
        }
        return res;

    }
4.e二叉树的最大深度46
public int maxDepth (TreeNode root) {
        // write code here
        if(root==null) return 0;
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
5.判断路径和55
public boolean hasPathSum (TreeNode root, int sum) {
        // write code here
        if(root==null && sum==0) return false;
        if(root==null) return false;
       
        if(root.left==null && root.right==null && root.val==sum) return true;
        return hasPathSum(root.left,sum-root.val) ||
        hasPathSum(root.right,sum-root.val);
    }
6. 改造树为双向链表16
 public TreeNode Convert(TreeNode pRootOfTree) {

        if(pRootOfTree==null) return null;

        trace(pRootOfTree);

        while(pRootOfTree.left!=null){
            pRootOfTree=pRootOfTree.left;
        }
        return pRootOfTree;

    }
    TreeNode pre=null;
    private void trace(TreeNode root) {
        // TODO
        if (root == null) return;

        trace(root.left);
        
        root.left=pre;
        
        if(pre !=null){
            pre.right=root;
        }
        pre=root;

        trace(root.right);
    }
7.判断是对称二叉树21
public boolean isSymmetrical (TreeNode pRoot) {
        // write code here
        if(pRoot==null) return true;
        if(pRoot.left==null && pRoot.right==null) return true;
        if(pRoot.left==null ) return false;
        if( pRoot.right==null) return false;

        return judge(pRoot.left,pRoot.right);
    }

    private boolean judge(TreeNode left, TreeNode right) {
        // TODO
        if(left==null && right==null) return true;
        if(left==null ) return false;
        if(right==null) return false;
        return left.val==right.val && judge(left.left,right.right)
        && judge(left.right,right.left);
    }
8.合并树25
public TreeNode mergeTrees (TreeNode left, TreeNode right) {
        // write code here
        if(left==null && right==null) return null;
        if(left==null ) return right;
        if(right==null) return left;
        TreeNode root=new TreeNode(left.val+right.val);
        root.left=mergeTrees(left.left,right.left);
        root.right=mergeTrees(left.right,right.right);
        return root;

    }
9.判断是否是镜像树28
public TreeNode Mirror (TreeNode pRoot) {
        // write code here
        if(pRoot==null) return null;
        TreeNode temp= pRoot.left;
        pRoot.left =    Mirror(pRoot.right);
        pRoot.right = Mirror(temp);

        return pRoot;
    }
10.判断是否是搜索二叉树36
public boolean isValidBST (TreeNode root) {
        // write code here
        if (root == null) return true;
        return judge(root,  Integer.MIN_VALUE, Integer.MAX_VALUE);
    }

    private boolean judge(TreeNode root, int left, int right) {
        // TODO
        if (root == null) return true;
        if (root.val <= left || root.val >= right) return false;

        return judge(root.left, left, root.val) &&
               judge(root.right, root.val, right) ;
    }
11.判断是否是平衡二叉树47
 public boolean IsBalanced_Solution (TreeNode pRoot) {
        // write code here
        if (pRoot == null) return true;
        if (Math.abs(maxDepth(pRoot.left)- maxDepth(pRoot.right)) > 1) return false;
        return IsBalanced_Solution(pRoot.left)
               && IsBalanced_Solution(pRoot.right);
    }

    public int maxDepth (TreeNode root) {
        // write code here
        if (root == null) return 0;
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }

12.二叉搜索树的公共父节点54
 public int lowestCommonAncestor (TreeNode root, int p, int q) {
        // write code here
        if(root==null) return -1;
        if(!(p<root.val && q<root.val ||
        p>root.val && q>root.val)) return root.val;
        int k=lowestCommonAncestor(root.left,p,q);
        return (k!=-1?k:lowestCommonAncestor(root.right,p,q));
    }
13.二叉树的公共节点04
Map<TreeNode,TreeNode> m=new HashMap<>();
    TreeNode p1;
    TreeNode p2;
    public int lowestCommonAncestor (TreeNode root, int o1, int o2) {
        // write code here
        find(root,o1,o2);
        Set<TreeNode> t=new HashSet<>();
        t.add(p1);
        while(p1!=root){
            p1=m.get(p1);
            t.add(p1);
        }
        t.add(root);
        while(!t.contains(p2)){
            p2=m.get(p2);
        }
        return p2.val;
    }

    private void find(TreeNode root, int o1, int o2) {
        // TODO
        if(root==null) return;
        if(root.val==o1) p1=root;
        if(root.val==o2) p2=root;
        if(root.left!=null)
            m.put(root.left,root);
        if(root.right!=null)
             m.put(root.right,root);
        find(root.left,o1,o2);
        find(root.right,o1,o2);
    }
14.重建二叉树22
public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {
        // write code here
        
        return judge(preOrder,0,preOrder.length-1,
        vinOrder,0,vinOrder.length-1);
    }

    private int find(int[] vinOrder, int val,int c,int d) {
        // TODO
        for(int i=c;i<=d;i++){
            if(vinOrder[i]==val)
            return i;
        }
        return -1;
    }

    private TreeNode judge(int[] preOrder, int a, int b, int[] vinOrder, int c, int d) {
        // TODO
        if(a>b || c>d) return null;
        TreeNode root=new TreeNode(preOrder[a]);
        int index =find(vinOrder,root.val,c,d);
        
        root.left = judge(preOrder,a+1,index-c+a,vinOrder,c,index-1);
        root.right = judge(preOrder,index-c+a+1,b,vinOrder,index+1,d);
        return root;
    }
15.输出右视图 29
public int[] solve (int[] preOrder, int[] inOrder) {
        // write code here
        TreeNode root = reConstructBinaryTree(preOrder, inOrder);
        return levelOrder(root);
    }
    public TreeNode reConstructBinaryTree (int[] preOrder, int[] vinOrder) {
        // write code here

        return judge(preOrder, 0, preOrder.length - 1,
                     vinOrder, 0, vinOrder.length - 1);
    }

    private int find(int[] vinOrder, int val, int c, int d) {
        // TODO
        for (int i = c; i <= d; i++) {
            if (vinOrder[i] == val)
                return i;
        }
        return -1;
    }

    private TreeNode judge(int[] preOrder, int a, int b, int[] vinOrder, int c,
                           int d) {
        // TODO
        if (a > b || c > d) return null;
        TreeNode root = new TreeNode(preOrder[a]);
        int index = find(vinOrder, root.val, c, d);

        root.left = judge(preOrder, a + 1, index - c + a, vinOrder, c, index - 1);
        root.right = judge(preOrder, index - c + a + 1, b, vinOrder, index + 1, d);
        return root;
    }
    public int[] levelOrder (TreeNode root) {
        // write code here

        List<Integer> res = new ArrayList<>();
        if (root == null) return null;
        Queue<TreeNode> q = new LinkedList<>();
        q.offer(root);
        while (!q.isEmpty()) {
            int k = q.size();
            ArrayList<Integer> temp = new ArrayList<>();
            for (int i = 0; i < k; i++) {
                TreeNode t = q.poll();
                temp.add(t.val);
                if (t.left != null) q.offer(t.left);
                if (t.right != null) q.offer(t.right);
            }
            res.add(temp.get(temp.size()-1));
        }
        return res.stream().mapToInt(i->i).toArray();

    }
16.判断是否是完全二叉树 41
 public boolean isCompleteTree (TreeNode root) {

        if(root==null) return true;
        Queue<TreeNode> q=new LinkedList<>();
        q.offer(root);
        int deep=0;
        while(!q.isEmpty()){
            int k=q.size();
            ArrayList<Integer> temp=new ArrayList<>();
            deep++;
            int flag=0;
            for(int i=0;i<k;i++){
                TreeNode t=q.poll();
                temp.add(t.val);
               
                if(t.left!=null) {
                    q.offer(t.left);
                    if(flag==1) return false;
                }else{
                    flag=1;
                }
                if(t.right!=null) {
                    q.offer(t.right);
                    if(flag==1) return false;
                }else{
                    flag=1;
                }
                
            }
            if(temp.size()!=Math.pow(2,deep-1) && !q.isEmpty()){
                return false;
            }
        }
        return true;

    }
总结:

1. 一般的题目都能够用递归的方法解决

2. 递归的时候主要进行的也是判空的操作;

题目来源

牛客网在线编程_编程学习|练习题_数据结构|系统设计题库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值