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 非递归

  • 相关阅读:
    'index.js' does not match the corresponding name on disk: '. ode_modules
    onload()方法只能在body标签中调用吗?怎么调用多个多个方法?
    HTML5新属性在Google浏览器中不能显示的问题
    HTML引入JS、CSS的各种方法
    框架、架构、设计模式的区别
    npm 命令 --save 和 --save-dev 的区别
    软件开发的权限控制和权限验证
    报错:Something is already running on port 8000.
    $stateProvider resovle 无法找到的原因
    git 统计代码量 shell脚本
  • 原文地址:https://www.cnblogs.com/sunada2005/p/6131977.html
Copyright © 2011-2022 走看看