算法模板

时间:2021-08-30 03:38:14   收藏:0   阅读:12

各类算法基础模板

一、二分查找法


二、KMP


三、二叉树

// Definition for a binary tree node.
public class TreeNode {
     public int val;
     public TreeNode left;
     public TreeNode right;
     TreeNode() {}
     TreeNode(int val) { this.val = val; }
     TreeNode(int val, TreeNode left, TreeNode right) {
         this.val = val;
         this.left = left;
         this.right = right;
     }
 }

二叉树的深度优先遍历

广度优先遍历(队列)

可以直接解决如下题目:

二叉树深度

二叉树节点数量

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

四、回溯算法

原文:https://www.cnblogs.com/luedong/p/15202930.html

评论(0)
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!