Java–二叉树遍历
今天没有继续昨天安卓软件开发的学习(dddd),写了一个二叉树遍历的程序,如下:
package TraveralOfBinaryTree; public class Node {//创建树 private int data; private Node leftNode; private Node rightNode; public Node(int data,Node leftNode,Node rightNode){ this.data=data; this.leftNode=leftNode; this.rightNode=rightNode; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Node getLeftNode() { return leftNode; } public void setLeftNode(Node leftNode) { this.leftNode = leftNode; } public Node getRightNode() { return rightNode; } public void setRightNode(Node rightNode) { this.rightNode = rightNode; } }
package TraveralOfBinaryTree; public class BinaryTree { public Node init(){ Node J=new Node(8,null,null); Node H=new Node(4,null,null); Node G=new Node(2,null,null); Node F=new Node(7,null,J); Node E=new Node(5,H,null); Node D=new Node(1,null,G); Node C=new Node(9,F,null); Node B=new Node(3,D,E); Node A=new Node(6,B,C); return A; } public void printNode(Node node){ System.out.print(node.getData()); }//返回根节点 public void theFirstTraversal(Node root){//先序 printNode(root); if(root.getRightNode()!=null){ theFirstTraversal(root.getRightNode()); }//遍历左孩子 if(root.getRightNode()!=null){ theFirstTraversal(root.getRightNode()); }//遍历右孩子 } public void theInOrderTraversal(Node root){ if(root.getLeftNode()!=null){ theInOrderTraversal(root.getLeftNode()); } printNode(root); if(root.getRightNode()!=null){ theInOrderTraversal(root.getRightNode()); } }//中序遍历 public void thePostTraversal(Node root){//后序 if(root.getRightNode()!=null){ thePostTraversal(root.getRightNode()); }//遍历左孩子 if(root.getRightNode()!=null){ thePostTraversal(root.getRightNode()); }//遍历右孩子 printNode(root); } public static void main(String[] args){ BinaryTree tree=new BinaryTree(); Node root=tree.init(); System.out.println("先序遍历"); tree.theFirstTraversal (root); System.out.println(""); System.out.println("中序遍历"); tree.theInOrderTraversal (root); System.out.println(""); System.out.println("后序遍历"); tree.thePostTraversal (root); System.out.println(""); } }
package TraveralOfBinaryTree; import java.util.Stack; public class BinaryTree1 { public Node init(){ Node J=new Node(8,null,null); Node H=new Node(4,null,null); Node G=new Node(2,null,null); Node F=new Node(7,null,J); Node E=new Node(5,H,null); Node D=new Node(1,null,G); Node C=new Node(9,F,null); Node B=new Node(3,D,E); Node A=new Node(6,B,C); return A; } public void printNode(Node node){ System.out.print(node.getData()); }//返回根节点 public void theFirstTraversal_Stack(Node root) {//先序 Stack<Node> stack=new Stack<Node>(); Node node=root; while(node!=null||stack.size()>0){ if (node != null) { printNode(node); stack.push(node);//压栈 node=node.getLeftNode(); }else{ node=stack.pop();//出栈 node=node.getRightNode(); } }} public void theInOrderTraversal_Stack(Node root) {//中序 Stack<Node> stack=new Stack<Node>(); Node node=root; while(node!=null||stack.size()>0){ if (node != null) { stack.push(node);//压栈 node=node.getLeftNode(); }else{ node=stack.pop();//出栈 printNode(node); node=node.getRightNode(); } }} public void thePostTraversal_Stack(Node root) {//后序 Stack<Node> stack=new Stack<Node>(); Stack<Node> output=new Stack<Node>();//构造栈存逆序结果 Node node=root; while(node!=null||stack.size()>0){ if(node!=null){ output.push(node); stack.push(node); node=node.getRightNode(); }else{ node=stack.pop(); node=node.getLeftNode(); } } System.out.println(output.size()); while(output.size()>0){ printNode(output.pop()); } } public static void main(String[] args){ BinaryTree1 tree=new BinaryTree1(); Node root=tree.init(); System.out.println("先序遍历"); tree.theFirstTraversal_Stack (root); System.out.println(""); System.out.println("中序遍历"); tree.theInOrderTraversal_Stack (root); System.out.println(""); System.out.println("后序遍历"); tree.thePostTraversal_Stack(root); System.out.println(""); } }
明天如果不出意外的话会继续安卓软件的开发学习。
推荐这些文章:
import java.util.LinkedList;
import java.util.List;
public class BinaryTree{
private TreeNode root;
public BinaryTree(){}
public BinaryTree(TreeNode roo...
public class B {
public static final void main(String[] args) {
System.out.println(System.getProperty("bb"));
System.out.println(System.getProperty("file.enc...
介绍
The Stack class represents a last-in-first-out stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack.
示例
public ...
1 + 2 + "3" 与 1 + 2 + '3' 结果不同
代码如下:
public class Test{
public static void main(String[] args){
System.out.println(1 + 2 + "3");
System.out.println...
Java机试题*:火车进站(queue队列FIFO-stack栈FILO-deque双向队列/ 动态规划、递归)
描述
给定一个正整数N代表火车数量,0<N<10,接下来输入火车入站的序列,一共N辆火车,每辆火车以数字1-9编号,火车站只有一个方向进出,同时停靠在火车站的列车中,只有后进站的出站了,先进站的才能出站。
要求输出所有火车出站的方案,以字典序排序输出。
数据范围:1\le n\le 10\1≤n≤10
进阶:时...
1650. Lowest Common Ancestor of a Binary Tree III
The first solution of this problem can be based on the 236. Lowest Common Ancestor of a Binary Tree too:
public Node lowestCommonAncestor(Node p, Node q) ...
项目:vue2
在启动项目的时候可能会出现 node-sass 包报错,在确保node-sass、sass-load 、style-loader三个包之间的版本是匹配的时候,大部分原因是因为是电脑上node版本与项目里面的node-sass包之间的版本存在兼容问题。
附上:node版本与node-...
function Node(value) {
this.value = value;
this.left = null;
this.right = null;
}
let a = new Node("a");
let b = a.left = new Node("b");
let c = a.right = ...
深度优先搜索
class Solution {
List<Integer> list = new LinkedList<>();
public List<Integer> postorder(Node root) {
if (root == null){
...
今天把node.js 从 v12.16.3 升级到 v16.14.2 然后原来的程序系统出现了这个问题
先删除
npm uninstall node-sass
或
cnpm uninstall node-sass
...
文章链接:https://www.dianjilingqu.com/4068.html
本文章来源于网络,版权归原作者所有,如果本站文章侵犯了您的权益,请联系我们删除,联系邮箱:saisai#email.cn,感谢支持理解。