![](https://img-blog.csdnimg.cn/20201014180756922.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
算法和数据结构
bh_xiaoxinba
这个作者很懒,什么都没留下…
展开
-
二叉树系列---找到累加和为指定值的最长路径长度
题目给定一颗二叉树和一个sum,二叉树节点值为整数,求累加和为sum的最大路径长度; 路径是指从某个节点往下,每次最多选择一个孩子节点或者不选择所形成的节点链;思路已知头结点head和给定值sum;求maxLen(maxLen保存最大路径长度,整个过程中,需要比较maxLen,所以设定maxLen为变量);对于当前节点,求出头到该节点的累加和,并存入sumMap中; 判断在以该节点为结尾的情况下,原创 2016-11-04 01:31:25 · 1783 阅读 · 2 评论 -
二叉树系列---构造二叉查找树
构造二叉查找树package binary_tree;public class BuildBinarySearchTree { private Node root; public BuildBinarySearchTree(){ root=null; } public void insert(int data){ Node newNode=n原创 2016-11-02 12:19:43 · 347 阅读 · 0 评论 -
二叉树系列---求出二叉树的最小深度
求出二叉树的最小深度;分析: 空树:1 只有根:1 只有左子树或只有右子树:子树深度+1; 既有左,又有右子树:min(左右子树深度)+1;实现:public int minDepth(Node root){ if(root==null){ return 0; } int ldp=minDepth(root.left);原创 2016-11-02 17:57:54 · 255 阅读 · 0 评论 -
二叉树系列---求所有从根到叶子路径组成的数的和
题目:给定一个二叉树,从根到叶子的所经过的所有节点的值组成一个数,求所有路径数的和; 比如, 1 / \ 2 312+13=25实现:从上向下求,递归; 根:sum=val; 向下:sum=sum*10+val 如果是叶子,则返回; 非叶子,则左的和+右的和;代码: public int sumNumbers(Node node){ int原创 2016-11-02 20:06:52 · 1209 阅读 · 0 评论 -
二叉树系列---求二叉树的最大路径和
题目Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3Return6.思路/*对于给定节点,以该节原创 2016-11-02 21:23:21 · 690 阅读 · 0 评论 -
动态规划系列---求数组中两个元素差的最大值
题目 求数组中两个元素差的最大值(后面的元素减去前面的元素);对应实际生活中的股票买卖,找出可能的最大收益;思路 类似于求数组连续和的最大值; 保存最大差值和最小值,遍历数组,如果当前元素-min>最大差值,则更新最大差值;如果当前元素<最小值,则更新最小值;实现public static int max_difference(int[] a){ int len=a.lengt原创 2016-11-15 21:55:22 · 3988 阅读 · 0 评论 -
字符串系列---最长公共子串
题目 给定两个字符串,求两个字符串的最长公共字串;/* * 动态规划方法: * c[i][j]表示Xi和Yj的最大Substring的长度; * 初始化: * c[0][j]=0 * c[i][0]=0 * 动态转移方程: * 如果xi == yj, 则 c[i][j] = c[i-1][j-1]+1原创 2016-12-02 21:01:05 · 302 阅读 · 0 评论 -
字符串系列---longest-substring-without-repeating-characters
题目 Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for “abcabcbb” is “abc”, which the length is 3. F原创 2016-11-21 02:20:34 · 503 阅读 · 0 评论 -
字符串系列---最长回文字串
题目Given a string S, find the longest palindromic substring in S. * You may assume that the maximum length of S is 1000, * and there exists one unique longest palindromic substring. * 最长回文字串方法1暴原创 2016-11-23 16:07:04 · 246 阅读 · 0 评论 -
字符串系列---palindrome-partitioning
题目Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s =”aab”, Return [ [“aa”,”b原创 2016-11-24 01:18:57 · 388 阅读 · 0 评论 -
字符串系列---word search
题目 已知:一个二维字符矩阵,一个单词; 输出:该单词是否可以从二维矩阵中拼接出来?拼接规则:从矩阵的某一行的某个字母开始,持续向临近的字符扩展(向上,向下,向左,向右),直至拼接出该单词。若可以拼出,则输出false,若拼不出,则输出false; 例如: 【A,B,C,D,E E,F,G,H,U A,B,S,D,F 】 单词为ABCGH,输出true; 单词为FG原创 2016-10-14 14:19:36 · 295 阅读 · 0 评论 -
字符串系列---string-to-integer-atoi
题目输入一个由数字组成的字符串,把它转换成整数并输出。例如:输入字符串”123”,输出整数123。给定函数原型int StrToInt(const char *str) ,实现字符串转换成整数的功能,不能使用库函数atoi。分析基本思路便是:从左至右扫描字符串,把之前得到的数字乘以10,再加上当前字符表示的数字:int StrToInt(const char *str){ while (*原创 2016-11-24 02:05:54 · 370 阅读 · 0 评论 -
二叉树系列---求二叉树中两个节点之间的最大距离
题目求二叉树中两个节点之间的最大距离(节点之间的距离:这两个节点之间边的个数); 计算一个二叉树的最大距离有两个情况:情况A: 路径经过左子树的最深节点,通过根节点,再到右子树的最深节点。 情况B: 路径不穿过根节点,而是左子树或右子树的最大距离路径,取其大者。 只需要计算这两个情况的路径距离,并取其大者,就是该二叉树的最大距离。 方法1求左子树到根的最大距离leftMaxDistance;原创 2016-11-02 16:46:45 · 1318 阅读 · 0 评论 -
二叉树序列---根据后序和中序系列或前序和中序序列构造二叉树
1、根据前序遍历序列和中序遍历序列,构造二叉树(或求后序遍历序列);public Node buildTree(int[] preOrder,int[] inOrder){ Node root=buildTree(preOrder,0,preOrder.length-1,inOrder,0,inOrder.length-1); return root; }原创 2016-11-02 15:13:44 · 621 阅读 · 0 评论 -
二叉树系列---基础
1、递归定义 二叉树或者是一颗空树,或者是以颗由一个根节点和两颗互不相交的分别成为根节点的左子树和右子树组成的非空树,左右子树也是一颗二叉树; 2、基本概念 叶节点; 分支节点; 深度; 度;完全二叉树; 满二叉树; 二叉排序树(二叉查找树)3、性质 非空二叉树的第i层最多的节点数; 深度为k的二叉树,最多包含的节点数,最少的节点数; 度为0的节点数n0,度为1的节点数n1,度原创 2016-11-02 11:31:57 · 279 阅读 · 0 评论 -
二叉树系列---在二叉树中找到两个节点的最近公共祖先
题目给定一颗二叉树的头结点,和这颗二叉树中2个节点n1和n2,求这两个节点的最近公共祖先;思路利用后序遍历实现; 对于当前节点cur,如果节点为null或者等于n1或n2中的一个,则直接返回cur; 先处理左右子树,左子树返回left,右子树返回right;判断left和right; 1)left和right均为null,说明以cur为根的树上没发现n1和n2; 2)left和right均不原创 2016-11-04 02:11:59 · 2756 阅读 · 0 评论 -
二叉树系列---标记二叉树中每个节点的next指针
题目Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If t原创 2016-11-04 04:07:02 · 1873 阅读 · 0 评论 -
二叉树系列---path-sum
题目Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree andsum原创 2016-11-04 04:21:26 · 350 阅读 · 0 评论 -
二叉树系列---path-sum-II
题目Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. For example: Given the below binary tree andsum = 22, return [ [5,4,11,原创 2016-11-04 04:36:27 · 379 阅读 · 0 评论 -
二叉树系列---判断二叉树是否为平衡二叉树
题目Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ原创 2016-11-04 04:53:44 · 242 阅读 · 0 评论 -
二叉树系列---求包含n个节点的二叉查找树的种类数
题目求包含n个节点的二叉树的种类数;方法1设dp[i]表示共有i个节点时,能产生的BST树的个数 n == 0 时,空树的个数必然为1,因此dp[0] = 1 n == 1 时,只有1这个根节点,数量也为1,因此dp[1] = 1 当根节点元素为 1, 2, 3, 4, 5, …, i, …, n时,基于以下原则的BST树具有唯一性:原创 2016-11-05 15:19:26 · 5687 阅读 · 1 评论 -
二叉树系列---判断给定的二叉树是否为二叉查找树
题目判断给定的二叉树是否为二叉查找树方法1递归判断:先判断左子树是否为二叉查找树;再判断右子树;最后判断根(根小于左子树的最大节点值,大于右子树的最左节点值);public boolean isValidBST(TreeNode root){ if(root==null){ return true; } if(root.left原创 2016-11-05 16:40:24 · 810 阅读 · 0 评论 -
二叉树系列---recover_binary_search_tree
题目在一个二叉查找树中,有两个节点被错误的交换了,实现一个函数得出正确的二叉查找树;思路利用二叉查找树性质:中序遍历有序; *先得到中序序列; * 正向变量中序序列,遇到逆序的就break; * 反向遍历中序序列,遇到逆序的就break; * 交换两个break处的元素即可;实现public void recoverTree(TreeNode root){原创 2016-11-05 17:44:51 · 301 阅读 · 0 评论 -
二叉树系列---same-tree
题目 给定两颗二叉树,判断这两颗二叉树是否相同; Two binary trees are considered equal if they are structurally identical and the nodes have the same value.实现 递归:判断根节点的值;判断左子树和右子树;public boolean isSameTree(TreeNode p, Tree原创 2016-11-05 17:59:48 · 261 阅读 · 0 评论 -
二叉树系列---层次遍历二叉树
层次遍历借助队列public void layerTranverse(Node root){ if(root==null){ return; } Queue<Node> queue=new LinkedList<Node>(); queue.add(root); while(!queue.isEmp原创 2016-11-02 14:03:08 · 298 阅读 · 0 评论 -
二叉树系列---symmetric_tree
题目Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3But the follow原创 2016-11-05 22:25:56 · 298 阅读 · 0 评论 -
字符串系列---字符串的统计字符串
题目求字符串中的统计字符串。即,对于字符串”aaabbadddffc”,返回“a_3_b_2_a_1_d_3_f_2_c_1”;分析记录变量 sb(最终结果);num每个连续字符的次数; * 初始化变量:sb中加入第一个字符,num=1; * 从1开始遍历str,遍历到第i位置; * 判断str[i]==str[i-1] 则表示连续字符还没有数完,nu原创 2016-11-26 14:53:19 · 704 阅读 · 0 评论