首页 > 其他分享 >二叉树遍历

二叉树遍历

时间:2023-10-12 20:55:54浏览次数:26  
标签:node 遍历 TreeNode System 二叉树 null stack

package com.exe4.offer;

import java.util.Stack;

/**
* 前序、中序、后序遍历方法
* @author WGS
*
*/
public class BianliOfBinarryTree {
public static class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;

}
}
/**
* 方法一:大话数据结构中简便方法,可实现操作。
* @param args
*/

public void preOrder(TreeNode node){
if(node==null) return;
System.out.print(node.val+" ");
preOrder(node.left);
preOrder(node.right);
}
public void inOrder(TreeNode node){
if(node==null) return;
inOrder(node.left);
System.out.print(node.val+" ");
inOrder(node.right);
}
public void postOrder(TreeNode node){
if(node==null) return;
postOrder(node.left);
postOrder(node.right);
System.out.print(node.val+" ");
}
/**
* 方法二:剑指offer中的方法:加入栈操作
* @param args
*/
public void preOrder2(TreeNode node){
Stack<TreeNode> stack=new Stack<TreeNode>();

while(node!=null || !stack.empty()){
//先遍历左子树
while(node!=null){
System.out.print(node.val+" ");
stack.push(node);
node=node.left;//左子树遍历结束后,node==null,跳出此循环
}
//左子树遍历结束,继续遍历右子树
if(!stack.isEmpty()){
node=stack.pop();//一直返回,直到根节点处,node==null,即stack==isEmpty()
node=node.right;
}
}
}
public void inOrder2(TreeNode node){
Stack<TreeNode> stack=new Stack<TreeNode>();
while(node!=null || !stack.isEmpty()){

while(node!=null){
stack.push(node);//8 6 5
node=node.left; //6 5 null
}

if(!stack.isEmpty()){
node=stack.pop();//5 6
System.out.print(node.val+" ");//5 6
node=node.right;//null
}
}
}
//后序遍历暂时还不会
/*public void postOrder2(TreeNode node){
Stack<TreeNode> stack=new Stack<TreeNode>();
while(node!=null || !stack.isEmpty()){
while(node!=null){
stack.push(node);//8 6 5 7
node=node.left;//6 5 null null
}
if(!stack.isEmpty()){
node=stack.pop();//5 6 7
node=node.right;//null 7 null

}

}
}*/

public static void main(String[] args) {
TreeNode root = new TreeNode(8);
TreeNode node1 = new TreeNode(6);
TreeNode node2 = new TreeNode(10);
TreeNode node3 = new TreeNode(5);
TreeNode node4 = new TreeNode(7);
TreeNode node5 = new TreeNode(9);
TreeNode node6 = new TreeNode(11);

root.left = node1;
root.right = node2;
node1.left = node3;
node1.right = node4;
node2.left = node5;
node2.right = node6;

BianliOfBinarryTree bianli=new BianliOfBinarryTree();
bianli.preOrder(root);
System.out.println("《《前序遍历-------");
bianli.inOrder(root);
System.out.println("《《中序遍历-------");
bianli.postOrder(root);
System.out.println("《《后序遍历-------");
bianli.preOrder2(root);
System.out.println("方法二:《《前序遍历-------");
bianli.inOrder2(root);
System.out.println("方法二:《《中序遍历-------");
// bianli.postOrder2(root);
System.out.println("方法二:《《后序遍历-------");
}

}

标签:node,遍历,TreeNode,System,二叉树,null,stack
From: https://www.cnblogs.com/yingpu/p/17760522.html

相关文章

  • 王道408---DS---树、二叉树、图
    有序树、无序树的概念有序树和无序树,树中结点的各子树从左到右是有次序的,不能互换,称该树为有序树,否则称为无序树。树/二叉树的性质树的性质常用的只有第一个二叉树的性质常用公式也只有这一个二叉树的存储一般分为顺序存储与链式存储要求顺序存储能默写顺序存储:typ......
  • 我汤姆回来了(树和图的深度优先遍历(树的重心))(10/11)
    #include<iostream>#include<cstring>usingnamespacestd;constintN=100010;constintM=N*2;//可能多次节点重复,所以开大intn;inte[M],ne[M],h[N],idx=0;boolst[N];intans=N;//记录最后最小值答案//单链表的连接,不同点就是头结点有多个voidadd(i......
  • HashMap-二叉树
        ......
  • LeetCode101.对称二叉树
    classSolution{//ArrayDeque不支持添加nullpublicbooleanisSymmetric(TreeNoderoot){returndfs(root.left,root.right);}//实际上,递归比较的就是根节点左右子树上,对称位置的节点booleandfs(TreeNodeleft,TreeNoderight){i......
  • 10.10树的遍历
    publicclassMain{publicstaticvoidmain(String[]args){trees<Integer>t=newtrees<>(1);trees<Integer>t1=newtrees<>(2);trees<Integer>t2=newtrees<>(3);trees<Integer>t3=ne......
  • HashMap-二叉树
        ......
  • jquery获取radio选中值及遍历
    在一个表单中我们通常是要获取被选中的那个radio项的值,所以要加checked来筛选,比如有以下的一些radio项:<inputtype="radio"name="testradio"value="jquery获取radio的值"/><inputtype="radio"name="testradio"value="jquery获取checkbox的值&quo......
  • TreeAPI 递归和非递归遍历
    只包含递归和非递归遍历#include<stdio.h>#include<stdlib.h>#defineMaxSize20typedefstructnode{intdata;structnode*lchild,*rchild;}TreeNode,*Tree;typedefTreeNode*Elem;//方便修改栈中元素类型typedefstruct{Elemdata......
  • 在JavaScript中遍历数组的循环(对于每个)
    内容来自DOChttps://q.houxu6.top/?s=在JavaScript中遍历数组的循环(对于每个)我可以使用JavaScript遍历数组中的所有条目吗?TL;DR你最好选择通常的方法是:使用for-of循环(ES2015+只支持;规范|MDN)-简单且适用于async。for(constelementoftheArray){//.......
  • 143-3 二叉树后序非递归遍历
    二叉树的后序非递归遍历使用辅助栈 r指针的作用是判断该结点是否遍历过#include<stdio.h>#include<stdlib.h>#defineMaxSize20typedefstructnode{intdata;structnode*lchild,*rchild;}TreeNode,*Tree;typedefTreeNode*Elem;typedefstruct{......