![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
面试算法
文章平均质量分 63
dongqifan
这个作者很懒,什么都没留下…
展开
-
最近点问题
package divideconquer;import java.util.Arrays;import java.util.Comparator;class Point{ int x; int y; public Point(int x, int y) { this.x = x; this.y = y; }}public class ClosestPairofPoi原创 2014-07-13 16:02:29 · 409 阅读 · 0 评论 -
查看一颗二叉树的每个节点的值是否都是其左右两个孩子节点值的和
package tree;public class SumProperty { /** * 查看一颗二叉树的每个节点的值是否都是其左右两个孩子节点值的和 * 当有一个孩子节点为空时,节点值为0 * @param args */ public static boolean issumproperty(TreeNode root){ if(root==null||(roo原创 2014-06-30 12:51:30 · 906 阅读 · 0 评论 -
求二叉树的宽度
package tree;import java.util.LinkedList;public class WithOfTree { /** * 求二叉树的宽度,其实就是层次遍历的改进 * @param args */ public static int getWith(TreeNode root){ if(root==null) return 0; Linked原创 2014-06-30 20:03:07 · 575 阅读 · 0 评论 -
DoubleTree
package tree;public class DoubleTree { /** * Write a program that converts a given tree to its Double tree. * To create Double tree of the given tree, create a new duplicate for each node,原创 2014-06-30 19:51:19 · 642 阅读 · 0 评论 -
根据前序和中序数组构造二叉树(递归方法)
package tree;public class ConstructTree { /** * 根据前序和中序数组构造二叉树 * @param args */ public static int idx = 0; public static TreeNode construct(int[] pre,int[] in, int left,int right){ if(l原创 2014-06-30 19:32:01 · 437 阅读 · 0 评论 -
判断一颗二叉树是否平衡
package tree;class height{ int h;}public class Isbanabalanced { /** * 判断一颗二叉树是不是平衡的二叉树,本质是采用后序遍历 * @param args */ public static boolean isbalance(TreeNode root){ if(root==null) return原创 2014-06-30 16:56:42 · 414 阅读 · 0 评论 -
求解一颗二叉树中两个叶子节点最长的路径
package tree;class Res{ int max;}public class DiameterofaBinary { /** * 求解一颗二叉树中两个叶子节点最长的路径 * @param args */ public static int getDiameter(TreeNode root,Res rs){ if(root==null) return原创 2014-06-30 16:46:05 · 1503 阅读 · 0 评论 -
Connect nodes at same level using constant extra space
Connect nodes at same level using constant extra space原创 2014-07-01 09:33:03 · 506 阅读 · 0 评论 -
打印二叉树中某个节点的所有父节点
package tree;public class PrintAncestorsofagivenode { /** * 打印二叉树中某个节点的所有父节点 * @param args */ public static boolean printan(TreeNode root,int num){ if(root==null) return false; if(root.原创 2014-06-30 20:55:10 · 1082 阅读 · 0 评论 -
Check if a given Binary Tree is SumTree
package tree;class Ress{int sum;}public class SumTree {/*** Following is an example of SumTree. 26 / \ 10 3 / \ \ 4 6原创 2014-06-30 21:35:24 · 405 阅读 · 0 评论 -
Vertical Sum in a given Binary Tree
package tree;import java.util.HashMap;import java.util.Set;public class VerticalSum { /** * Vertical Sum in a given Binary TreeGiven a Binary Tree, find vertical sum of the nodes that are in原创 2014-07-01 10:24:57 · 485 阅读 · 0 评论 -
平衡二叉搜索树(AVL)插入和删除的java代码实现
AVL树节点的声明代码:package geeksgeeks.avl;public class TreeNode { int key ; TreeNode left ; TreeNode right ; int height ; public TreeNode( int ke原创 2014-06-17 17:24:22 · 660 阅读 · 0 评论 -
判断一颗二叉树是不是查找二叉树
package bst;public class isBSTornot { /** * 本质用中序遍历 * @param args */ public static TreeNode pre = null; public static boolean isbst(TreeNode root){ if(root==null) return true; if(!isbs原创 2014-07-04 13:32:05 · 468 阅读 · 0 评论 -
用俩个栈实现二叉树的非递归后序遍历
package tree;import java.util.Stack;public class IterativePostorderTraversal { /** * 用一个栈实现二叉树的非递归后序遍历 * @param args */ public static void printpost(TreeNode root){ Stack stack1 = new S原创 2014-07-01 19:18:04 · 720 阅读 · 0 评论 -
Construct Full Binary Tree from given preorder and postorder traversals
package tree;public class ConstructFullBinaryTree { /** * Construct Full Binary Tree from given preorder and postorder traversalsGiven two arrays that represent preorder and postorder traversal原创 2014-07-01 15:08:56 · 480 阅读 · 0 评论 -
只用一个栈实现二叉树的非递归后序遍历
package tree;import java.util.Stack;public class IterativePostorderTraversalone { /** * 只用一个栈实现二叉树的非递归后序遍历 * @param args */ public static void printpost(TreeNode root){ Stack stack = ne原创 2014-07-01 19:49:27 · 790 阅读 · 0 评论 -
带栈的二叉树非递归先序遍历
package tree;import java.util.Stack;public class IterativePreorderTraversal { /** * 带栈的二叉树前序非递归遍历 * @param args */ public static void printpre(TreeNode root){ Stack stack = new Stack<>(原创 2014-07-01 15:49:32 · 414 阅读 · 0 评论 -
Construct a special tree from given preorder traversal
package tree;public class Construct { /** * * Construct a special tree from given preorder traversalGiven an array ‘pre[]‘ that represents Preorder traversal of a spacial binary tree where原创 2014-07-01 13:54:55 · 421 阅读 · 0 评论 -
Construct Special Binary Tree from given Inorder traversal
package tree;public class ConstructSpecialBinary { /** * Construct Special Binary Tree from given Inorder traversalGiven Inorder Traversal of a Special Binary Tree in which key of every node is原创 2014-07-01 11:09:27 · 352 阅读 · 0 评论 -
带栈的二叉树的非递归中序遍历
1 / \ 2 3 / \ 4 5package tree;import java.util.Stack;public class InorderTreeTraversal { /** * 带栈的二叉树的非递归中序遍历 * @param args */ public static原创 2014-06-30 17:08:45 · 508 阅读 · 0 评论 -
Convert a given tree to its Sum Tree
package tree;class Ressum{ int sum;}public class ConvertagiventreetoitsSumTree { /** * Convert a given tree to its Sum TreeGiven a Binary Tree where each node has positive and negative values原创 2014-07-01 10:09:09 · 396 阅读 · 0 评论 -
不带栈的二叉树中序非递归遍历
package tree;public class InorderTree { /** * 不带栈的二叉树中序非递归遍历 * 1. Initialize current as root 2. While current is not NULL If current does not have left child a) Print current’s data原创 2014-06-30 17:39:34 · 460 阅读 · 0 评论 -
将一颗二叉树转换成它的镜像
package tree;public class MirrorTree { /** * 将一颗二叉树转换成它的镜像 * @param args */ public static void convertmirror(TreeNode root){ if(root==null||(root.left==null&&root.right==null)) return;原创 2014-06-29 00:39:22 · 796 阅读 · 0 评论 -
删除一颗二叉树
package tree;public class DeleteBT { /** * 删除一颗二叉树 * @param args */ public static TreeNode delete(TreeNode root){ if(root==null) return root; root.left = delete(root.left); root.right原创 2014-06-29 00:29:19 · 1030 阅读 · 0 评论 -
求一颗二叉树的深度
t)); }}原创 2014-06-29 00:22:40 · 654 阅读 · 0 评论 -
判断两颗二叉树是否相同
package tree;public class IsIdentical { /** * 判断两棵树是否相同 * @param args */ public boolean isidentical(TreeNode root1,TreeNode root2){ if(root1==null||root2==null){ if(root1!=null){ r原创 2014-06-29 00:14:28 · 597 阅读 · 0 评论 -
Prim最小生成树的最小堆的java代码实现
package prim;public class MinHeapNode { int v ; int weight ; public MinHeapNode( int v, int weight) { this .v = v; this .weight = weight;原创 2014-06-28 21:13:18 · 736 阅读 · 0 评论 -
最长公共子序列
public class Zuichanggonggongzixulie { static void pirntlcs(char[] a,char[] b){ int s = b.length+1; int e = a.length+1; int[][]temp = new int[s][e]; for(int i=0;i<s;i++){ for(int j=原创 2014-03-21 10:51:01 · 430 阅读 · 0 评论 -
荷兰国旗问题
public class helanguoqi { static void paixu(int[] a){ int begin = 0; int end = a.length-1; int current = 0; while(current<=end){ if(a[current]==0){ int temp = a[begin]; a[beg原创 2014-03-21 10:52:22 · 379 阅读 · 0 评论 -
打印字符串的全排列
public class Pailiezuhe { public static void dayin(char[] s,int st){ if(s==null||s[0]==' '){ return; } int start = st; int end = st; if(start==s.length){ for(int i=0;i<s.l原创 2014-03-21 10:23:34 · 382 阅读 · 0 评论 -
统计二叉树叶子节点的个数
package tree;public class CountleafNode { /** * 统计二叉树中叶子节点的个数 * @param args */ public static int count(TreeNode root){ if(root==null) return 0; int sum = 0; if(root.left==null&&root.r原创 2014-06-29 10:21:26 · 2949 阅读 · 0 评论 -
Kruskal生成树算法的java代码简单实现
package kruskal;import java.util.Arrays;import java.util.Comparator;import java.util.LinkedList;import java.util.List;class EdgeCom implements Comparator{ @Override public int compare(EdgeNo原创 2014-06-28 15:26:33 · 405 阅读 · 0 评论 -
Get Level of a node in a Binary Tree
package tree;public class GetLevelofanode { /** * 求二叉树某个节点所在的层数,如果没有就返回0 * @param args */ public static int getlevel(TreeNode root,int num,int level){ if(root==null) return 0; if(root.v原创 2014-06-30 20:45:03 · 407 阅读 · 0 评论 -
Foldable Binary Trees
package tree;public class FoldableBinaryTrees { /** * Consider the below trees:(a) and (b) can be folded.(c) and (d) cannot be folded.(a) 10 / \ 7 15 \ /原创 2014-06-30 20:23:02 · 316 阅读 · 0 评论 -
查看一颗二叉树从根到叶子路径的节点和是否等于某个给定的数
package tree;public class Roottoeafpathsumequaltoagivennumber { /** * 查看一颗二叉树从根到叶子路径的节点和是否等于某个给定的数 * @param args */ public static boolean isexit(TreeNode root,int num,int sum){ if(root==n原创 2014-06-30 18:24:50 · 596 阅读 · 0 评论 -
打印二叉树中距离根节点为k的所有节点
package tree;public class Printnodesatkdistancefromroot { /** * Given a root of a tree, and an integer k. Print all * the nodes which are at k distance from root.For example, in the below t原创 2014-06-30 20:34:40 · 674 阅读 · 0 评论 -
将任意一颗二叉树转变为每一个节点都是另外两个孩子节点的和
50 / \ / \ 8 2 / \ /\ / \ / \ 3 5 1 30原创 2014-06-30 16:21:02 · 574 阅读 · 0 评论 -
二叉树层次遍历的螺旋打印
如图su原创 2014-06-29 10:39:11 · 864 阅读 · 0 评论 -
打印二叉树从根节点到叶子节点的所有路径
package tree;import java.util.LinkedList;public class TreePath { /** * 打印二叉树从根节点到叶子节点的所有路径 * @param args */ public static void getPath(TreeNode root){ if(root==null) return; LinkedLis原创 2014-06-29 00:54:36 · 5703 阅读 · 1 评论 -
Prim算法的简单实现
package prim;/** * 图的邻接矩阵实现的Prim算法 * @author Administrator * */public class Prim { public void getPrim(int[][] g,int v){ boolean mstset[] = new boolean[v]; int parent[] = new int[v]; i原创 2014-06-28 16:23:55 · 437 阅读 · 0 评论