zoukankan      html  css  js  c++  java
  • 轻松搞定面试中的二叉树题目(java&python)

    树是一种比较重要的数据结构,尤其是二叉树。二叉树是一种特殊的树,在二叉树中每个节点最多有两个子节点,一般称为左子节点和右子节点(或左孩子和右孩子),并且二叉树的子树有左右之分,其次序不能任意颠倒。二叉树是递归定义的,因此,与二叉树有关的题目基本都可以用递归思想解决,当然有些题目非递归解法也应该掌握,如非递归遍历节点等等。本文努力对二叉树相关题目做一个较全的整理总结,希望对找工作的同学有所帮助。

    二叉树节点定义如下:

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int x){
            val = x;
            left = null;
            right = null;
        }
    }
    View Code

    题目列表:

    1. 求二叉树中的节点个数
    2. 求二叉树的深度
    3. 前序遍历,中序遍历,后序遍历
    4.分层遍历二叉树(按层次从上往下,从左往右)
    5. 将二叉查找树变为有序的双向链表
    6. 求二叉树第K层的节点个数
    7. 求二叉树中叶子节点的个数
    8. 判断两棵二叉树是否结构相同
    9. 判断二叉树是不是平衡二叉树
    10. 求二叉树的镜像
    11. 求二叉树中两个节点的最低公共祖先节点
    12. 求二叉树中节点的最大距离
    13. 由前序遍历序列和中序遍历序列重建二叉树
    14.判断二叉树是不是完全二叉树

    按知识点分类

    1. 二叉树的遍历。

    1.1 深度优先遍历。可解答二叉树中的节点个数、前序遍历,中序遍历,后序遍历等问题。

    1.1.1 递归

    public void preOrder(ArrayList<Integer> res, TreeNode node){
            if(node == null){
                return;
            }
            res.add(node.val);
            preOrder(res, node.left);
            preOrder(res, node.right);
        }
    
        public void inOrder(ArrayList<Integer> res, TreeNode node){
            if(node == null){
                return;
            }
            inOrder(res, node.left);
            res.add(node.val);
            inOrder(res, node.right);
        }
    
        public void postOrder(ArrayList<Integer> res, TreeNode node){
            if(node == null){
                return;
            }
            postOrder(res, node.left);
            postOrder(res, node.right);
            res.add(node.val);
        }
    View Code

    1.1.2 非递归

    public ArrayList<Integer> preOrderStack(TreeNode node){
            if(node == null){
                return null;
            }
            ArrayList<Integer> res = new ArrayList<Integer>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode p = node;
            while(p != null || !stack.isEmpty()){
                while(p != null){
                    res.add(p.val);
                    stack.push(p);
                    p = p.left;
                }
                if(!stack.isEmpty()) {
                    p = stack.pop();
                    p = p.right;
                }
            }
            return res;
        }
    
        public ArrayList<Integer> inOrderStack(TreeNode node){
            if(node == null){
                return null;
            }
            ArrayList<Integer> res = new ArrayList<Integer>();
            Stack<TreeNode> stack = new Stack<TreeNode>();
            TreeNode p = node;
            while(p != null || !stack.isEmpty()){
                while(p != null){
                    stack.push(p);
                    p = p.left;
                }
                if(!stack.isEmpty()){
                    p = stack.pop();
                    res.add(p.val);
                    p = p.right;
                }
            }
            return res;
        }
    
        public ArrayList<Integer> postOrderStack(TreeNode node){
            if(node == null){
                return null;
            }
            TreeNode p;
            TreeNode pre = null;
            Stack<TreeNode> stack = new Stack<TreeNode>();
            ArrayList<Integer> res = new ArrayList<Integer>();
            stack.push(node);
            while(!stack.isEmpty()){
                p = stack.peek();
                if((p.left == null && p.right == null) || (pre != null && (pre == p.right || pre == p.left))){
                    res.add(p.val);
                    pre = p;
                    stack.pop();
                }else{
                    if(p.right != null){
                        stack.push(p.right);
                    }
                    if(p.left != null){
                        stack.push(p.left);
                    }
                }
            }
            return res;
        }
    View Code

    1.2 广度优先遍历。可解答分层遍历二叉树(按层次从上往下,从左往右)等问题。

    1.2.1 递归

    1.2.2 非递归

  • 相关阅读:
    Visual Studio 连接不上NuGet 官方程序包源的解决办法
    Microsoft Toolkit 2.5下载 – 一键激活Windows 8.1/2012 R2/Office 2013
    Intel® Ethernet Connection I217-V 网卡驱动(win10 ,2012)
    flashfxp3.41中文版注册码:(适合最新版本)
    Fiddler对安卓应用手机抓包图文教程
    网址检查器1.0
    Win10打不开chm文件的解决办法
    Android HTTP通讯
    对Android开发者有益的40条优化建议
    IIS下PHP的三种配置方式比较
  • 原文地址:https://www.cnblogs.com/sunada2005/p/6131977.html
Copyright © 2011-2022 走看看