二叉樹的前 中 後序遍歷

2021-10-01 09:49:30 字數 1187 閱讀 3364

import lombok.data;

import lombok.noargsconstructor;

@data

@noargsconstructor

class

treenode

/** * 前序遍歷

* 根-左-右

*/public

void

preorder()

//遞迴向右子樹前序遍歷if(

this

.right != null)

}/**

* 中序遍歷

* 左-根-右

*/public

void

infixorder()

system.out.

println

(value +

" ")

;//遞迴向右子樹前序遍歷if(

this

.right != null)

}/**

* 後序遍歷

* 左-右-根

*/public

void

postorder()

//遞迴向右子樹前序遍歷if(

this

.right != null)

system.out.

println

(value +

" ");}

}/**

* 二叉樹

*/@data

public

class

binarytree

public

void

printpreorder()

this

.root.

preorder()

;}public

void

printinfixorder()

this

.root.

infixorder()

;}public

void

printpostorder()

this

.root.

postorder()

;}public

static

void

main

(string[

] args)

}

二叉樹的前中後序遍歷

秋招記錄 對一棵二叉樹進行遍歷,我們可以採取3種順序進行遍歷,分別是前序遍歷 中序遍歷和後序遍歷。這三種方式是以訪問父節點的順序來進行命名的。假設父節點是n,左節點是l,右節點是r,那麼對應的訪問遍歷順序如下 前序遍歷 中左右 n l r 中序遍歷 左中右 l n r 後序遍歷 左右中 l r n ...

二叉樹的前 中 後序遍歷

前序 根左右 中序 左根右 後序 左右根 前序遍歷 124563 中序遍歷 546213 後序遍歷 564231 package datastructure public class binarytreedemo class binarytree public binarytree hero roo...

二叉樹的前中後序遍歷

關於二叉樹的遍歷,常見的有三種遍歷方式,即,前序遍歷,中序遍歷,後序遍歷,這個 序 字,即指當前的節點對於其左右子節點的先後處理關係。結合我們剛剛講的,就知道對於其左右子樹而言,優先遍歷當前的父節點,然後遍歷它的左子樹,最後遍歷它的右子樹。對於其左右子樹而言,當前父節點在中間遍歷,即先遍歷當前節點的...