将内容过程中经常用的内容做个记录,如下内容内容是关于Java递归方法遍历二叉树的内容。

package com.wzs;

public class TestBinaryTree {
public static void main(String[] args) {
Node<String> g = new Node<String>("G", null, null);
Node<String> e = new Node<String>("E", null, null);
Node<String> f = new Node<String>("F", null, null);
Node<String> d = new Node<String>("D", null, g);
Node<String> b = new Node<String>("B", d, e);
Node<String> c = new Node<String>("C", null, f);
Node<String> a = new Node<String>("A", b, c);

System.out.println("生成的二叉树:");
System.out.println(" A");
System.out.println(" | ");
System.out.println(" |---------|");
System.out.println(" B C");
System.out.println(" | |");
System.out.println(" |---------| -----|");
System.out.println(" D E F");
System.out.println(" |");
System.out.println(" ----|");
System.out.println(" G");

System.out.println("二叉树深度:" + BinaryTree.getDepth(a));

System.out.print("前序遍历:");
BinaryTree.priorderTraversal(a);
System.out.println();

System.out.print("中序遍历:");
BinaryTree.inorderTraversal(a);
System.out.println();

System.out.print("后序遍历:");
BinaryTree.postorderTraversal(a);
System.out.println();
}
}

class BinaryTree {
static <T> void priorderTraversal(Node<T> node) {
if (node != null) {
visitNode(node);
priorderTraversal(node.getLeftChild());
priorderTraversal(node.getRightChild());
}
}

static <T> void inorderTraversal(Node<T> node) {
if (node != null) {
inorderTraversal(node.getLeftChild());
visitNode(node);
inorderTraversal(node.getRightChild());
}
}

static <T> void postorderTraversal(Node<T> node) {
if (node != null) {
postorderTraversal(node.getLeftChild());
postorderTraversal(node.getRightChild());
visitNode(node);
}
}

static <T> int getDepth(Node<T> node) {
if (node == null) {
return 0;
}
int leftDepth = 0;
int rightDepth = 0;
leftDepth = getDepth(node.getLeftChild());
rightDepth = getDepth(node.getRightChild());
return (leftDepth > rightDepth ? leftDepth : rightDepth) + 1;
}

static <T> void visitNode(Node<T> node) {
System.out.print(node.getKey() + " ");
}
}

class Node<T> {
private T key;
private Node<T> leftChild;
private Node<T> rightChild;

public Node() {

}

public Node(T key, Node<T> leftChild, Node<T> rightChild) {
super();
this.key = key;
this.leftChild = leftChild;
this.rightChild = rightChild;
}

public T getKey() {
return key;
}

public void setKey(T key) {
this.key = key;
}

public Node<T> getLeftChild() {
return leftChild;
}

public void setLeftChild(Node<T> leftChild) {
this.leftChild = leftChild;
}

public Node<T> getRightChild() {
return rightChild;
}

public void setRightChild(Node<T> rightChild) {
this.rightChild = rightChild;
}

}

输出结果生成的二叉树:A||---------|BC|||---------|-----|DEF|----|G二叉树深度:4前序遍历:ABDGECF中序遍历:DGBEACF后序遍历:GDEBFCA

Java递归方法遍历二叉树的代码的更多相关文章

  1. 非递归遍历二叉树Java版的实现代码(没写层次遍历)

    直接上代码呵呵,里面有注解 package www.com.leetcode.specificProblem; import java.util.ArrayList; import java.util ...

  2. java创建二叉树并实现非递归中序遍历二叉树

    java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码 ...

  3. 《程序员代码面试指南》第三章 二叉树问题 遍历二叉树的神级方法 morris

    题目 遍历二叉树的神级方法 morris java代码 package com.lizhouwei.chapter3; /** * @Description:遍历二叉树的神级方法 morris * @ ...

  4. JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)

    import java.util.HashMap; import java.util.LinkedList; import java.util.Map; import java.util.Queue; ...

  5. Java遍历二叉树深度宽度

    节点数据结构 class TreeNode { TreeNode left = null; TreeNode right = null; } 最大深度,基本思路是:使用递归,分别求出左子树的深度.右子 ...

  6. JAVA递归、非递归遍历二叉树(转)

    原文链接: JAVA递归.非递归遍历二叉树 import java.util.Stack; import java.util.HashMap; public class BinTree { priva ...

  7. PHP递归方法实现前序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...

  8. java创建二叉树并递归遍历二叉树

    二叉树类代码: package binarytree; import linkqueue.LinkQueue; public class BinaryTree { class Node { publi ...

  9. 左神算法书籍《程序员代码面试指南》——3_05Morris遍历二叉树的神级方法【★★★★★】

    [问题]介绍一种时间复杂度O(N),额外空间复杂度O(1)的二叉树的遍历方式,N为二叉树的节点个数无论是递归还是非递归,避免不了额外空间为O(h),h 为二叉树的高度使用morris遍历,即利用空节点 ...

随机推荐

  1. Vuex的初探与实战

    1.背景 最近在做一个单页面的管理后台项目,为了提高开发效率,使用了Vue框架来开发.为了使各个部分的功能,独立结构更加清晰,于是就拆分了很多组件,但是组件与组件之间数据共享成了一个问题,父子组件实现 ...

  2. Github排序(转载)

    目录 1. 冒泡排序 2. 选择排序 3. 插入排序 4. 希尔排序 5. 归并排序 6. 快速排序 7. 堆排序 8. 计数排序 9. 桶排序 10. 基数排序 参考:https://mp.weix ...

  3. NFS挂载异常 mount.nfs: Input/output error

    [root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...

  4. powerDesigner生成数据结构图以及对应sql导出方法

    1.下载powerDesigner 链接地址为http://soft.onlinedown.net/soft/577763.htm 2.打开软件,file -> new project,新建一个 ...

  5. Linux系统监控命令详解

    1. top命令 top命令经常用来监控Linux的系统状况,比如cpu.内存的使用,程序员基本都知道这个命令,但比较奇怪的是能用好它的人却很少,例如top监控视图中内存数值的含义就有不少的曲解. 输 ...

  6. 实现iframe高度自适应

    iframe高度自适应使用场景是类似于微博,新闻等点击加载更多这种功能实现,要求iframe的高度能够跟随内容的变化而变化. 父html文件,也就是引用ifram的文件 src="blog/ ...

  7. web服务器,验证码,Xftp使用方法

    IIS操作步骤 直接装的wamp 腾讯云主机控制台 安全组里可以配置要开放的端口 关闭防火墙 (C:\wamp\bin\apache\Apache2.4.4) 打开httpd.conf文件 requi ...

  8. Servlet生命周期 、Filter生命周期、Listering(监听器)总结

    Servlet生命周期简述 (1)加载和实例化 当Servlet容器启动或客户端发送一个请求时,Servlet容器会查找内存中是否存在该Servlet实例,若存在,则直接读取该实例响应请求:如果不存在 ...

  9. 详细介绍Spring Boot 2.0的那些新特性与增强

    以Java 8 为基准 Spring Boot 2.0 要求Java 版本必须8以上, Java 6 和 7 不再支持. 内嵌容器包结构调整 为了支持reactive使用场景,内嵌的容器包结构被重构了 ...

  10. jquery mobile 建wap站

    使用jquery mobile 建手机wap站: 几篇比较好的文章 http://wap.yesky.com/dev/225/30974725.shtml http://tech.it168.com/ ...