八道二叉树基础程序面试题

一、二叉树的前序遍历

递归方法解决

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

非递归方法解决:利用栈

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

二、二叉树的中序遍历

递归

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

非递归

 public List<Integer> inorderTraversal(TreeNode root){
        List<Integer> list=new ArrayList();
        if(root==null){
            return list;
        }
        Stack<TreeNode> stack=new Stack<>();
        while(root!=null||!stack.isEmpty()){
            if(root!=null){
                stack.push(root);
                root=root.left;
            }
            else
            {
                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){
            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<Integer>();
    if(root == null)
        return list;
    Stack<TreeNode> stack = new Stack<TreeNode>();
    stack.push(root);
    while(!stack.isEmpty()){
        TreeNode node = stack.pop();
        if(node.left != null){
            stack.push(node.left);
        } 
        if(node.right != null) {
            stack.push(node.right);
        }
        list.add(0,node.val);                        //逆序添加结点值
    }     
    return list;
    }
}

四、判断两棵树是否相同

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

五、判断一棵树是不是另一棵树的子树

像这样就符合条件
在这里插入图片描述

class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
        if(s==null){
            return false;
        }
        //t是s的子树,要么t等于s,要么t等于s的左/右子树。
       return subFrom(s, t) || isSubtree(s.left, t) || isSubtree(s.right, t);
    }
    
    public boolean subFrom(TreeNode s, TreeNode t){
        if (s == null && t == null) return true;
        if (s == null || t == null) return false;
        if (s.val != t.val) return false;
        return subFrom(s.left, t.left) && subFrom(s.right, t.right);
    }
}

六、二叉树的深度

class Solution {
   public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
            }
            return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
            }
            
        }

七、判断一颗二叉树是否是平衡二叉树

平衡二叉树:节点度差的绝对值不超过1

class Solution {
  public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        // 左右子树的高度差的绝对值超过 1
        if (Math.abs(height(root.left) - height(root.right)) > 1) {
            return false;
        }
        // 分别递归判断左右子树
        if (!isBalanced(root.left)) {
            return false;
        }
        return isBalanced(root.right);
    }

    // 求节点的高度
    public int height(TreeNode root) {
        if (root == null) {
            return 0;
        } else {
            int left = height(root.left);
            int right = height(root.right);
            return Math.max(left,right) + 1;
        }
    }
}

八、对称二叉树

像这样的就叫对称二叉树。
在这里插入图片描述

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return cmp(root.left, root.right);
    }

    public boolean cmp(TreeNode node1, TreeNode node2) {
        if (node1 == null && node2 == null) {
            return true;
        }
        //这些情况都不会是对称的
        if (node1 == null || node2 == null || node1.val != node2.val) {
            return false;
        }
        return cmp(node1.left, node2.right) && cmp(node1.right, node2.left);
    }
}

猜你喜欢

转载自blog.csdn.net/chris__x/article/details/107818090