劍指offer-樹


import java.util.Queue;
import java.util.Stack;

/**
 * @ClassName tree
 * @Description
 *  * 遍歷二叉樹
 *  * 分別以遞歸和非遞歸實現(xiàn)前序惕澎、中序蚀之、后序遍歷二叉樹靴跛、與層級遍歷
 * @Version V1.0
 **/
public class tree {

  //遞歸
  //前序遍歷

  public void preOrder(Node node){
    if(node==null){
      return;
    }
    System.out.println("前序"+node.getValue());
    preOrder(node.getLeft());
    preOrder(node.getRight());
  }
  //中序遍歷
  public void inOrder(Node node){
    if(node == null){
      return;
    }
    inOrder(node.getLeft());
    System.out.println("中序"+node.getValue());
    inOrder(node.getRight());
  }
  //后序遍歷
 public void postOrder(Node node){
    if(node == null){
       return;
    }
    postOrder(node.getLeft());
    postOrder(node.getRight());
    System.out.println("后序"+node.getValue());
 }
  //非遞歸
  //前序遍歷-- 使用棧
  public void preOrderTraversal(Node node){
    if(node == null){
      return;
    }
    Stack<Node>  nodeStack  =  new Stack<>();
    nodeStack.push(node);
    while (!nodeStack.isEmpty()){
      Node cur_node = nodeStack.lastElement();
      nodeStack.pop();
      if(!nodeStack.isEmpty() && nodeStack.lastElement()==null){
        nodeStack.pop();
        System.out.println("前序"+cur_node.getValue());
        continue;
      }
      if(cur_node.getRight()!=null){
         nodeStack.push(cur_node.getRight());
      }
      if(cur_node.getLeft()!=null){
        nodeStack.push(cur_node.getLeft());
      }
      //前序 nullptr 標(biāo)記當(dāng)前節(jié)點的左右子樹已經(jīng)遍歷
      nodeStack.push(null);
      nodeStack.push(cur_node);
    }
  }
  //中序遍歷
  public void inOrderTraversal(Node node){
    if(node == null){
      return;
    }
    Stack<Node>  nodeStack  =  new Stack<>();
    nodeStack.push(node);
    while (!nodeStack.isEmpty()){
      Node cur_node = nodeStack.lastElement();
      nodeStack.pop();
      if(!nodeStack.isEmpty() && nodeStack.lastElement()==null){
        nodeStack.pop();
        System.out.println("中序"+cur_node.getValue());
        continue;
      }
      if(cur_node.getRight()!=null){
        nodeStack.push(cur_node.getRight());
      }
      //中序 nullptr 標(biāo)記當(dāng)前節(jié)點的左右子樹已經(jīng)遍歷
      nodeStack.push(null);
      nodeStack.push(cur_node);
      if(cur_node.getLeft()!=null){
        nodeStack.push(cur_node.getLeft());
      }

    }
  }
  //后序遍歷
  public void postOrderTraversal(Node node){
    if(node == null){
      return;
    }
    Stack<Node>  nodeStack  =  new Stack<>();
    nodeStack.push(node);
    while (!nodeStack.isEmpty()){
      Node cur_node = nodeStack.lastElement();
      nodeStack.pop();
      if(!nodeStack.isEmpty() && nodeStack.lastElement()==null){
        nodeStack.pop();
        System.out.println("后序"+cur_node.getValue());
        continue;
      }
      //后序 nullptr 標(biāo)記當(dāng)前節(jié)點的左右子樹已經(jīng)遍歷
      nodeStack.push(null);
      nodeStack.push(cur_node);
      if(cur_node.getRight()!=null){
        nodeStack.push(cur_node.getRight());
      }

      if(cur_node.getLeft()!=null){
        nodeStack.push(cur_node.getLeft());
      }

    }
  }

  //層級遍歷
    //實現(xiàn)過程
    //1、首先將二叉樹的根節(jié)點push到隊列中糟趾,判斷隊列不為NULL,就輸出隊頭的元素黎烈,
    //2唠粥、判斷節(jié)點如果有孩子,就將孩子push到隊列中蛉艾,
    //3钳踊、遍歷過的節(jié)點出隊列,
    //4勿侯、循環(huán)以上操作拓瞪,直到Tree == NULL。
  public void levelOrder(Node node){
    if(node ==null){
      return;
    }
    Queue<Node> queue = null;
    if(node!=null){
      queue.add(node);
    }
    while (!queue.isEmpty()){
      System.out.println(queue.peek().getValue());
      if(queue.peek().getLeft()!=null){
         queue.add(queue.peek().getLeft());
      }
      if(queue.peek().getRight()!=null){
        queue.add(queue.peek().getRight());
      }
      queue.poll();
    }
  }
}
class Node {

  private  int value;
  private  Node left;
  private  Node right;

  public Node(int value){
    this.value=value;
    this.left=null;
    this.right=null;
  }

  public int getValue() {
    return value;
  }

  public void setValue(int value) {
    this.value = value;
  }

  public Node getLeft() {
    return left;
  }

  public void setLeft(Node left) {
    this.left = left;
  }

  public Node getRight() {
    return right;
  }

  public void setRight(Node right) {
    this.right = right;
  }

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末罐监,一起剝皮案震驚了整個濱河市吴藻,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌弓柱,老刑警劉巖沟堡,帶你破解...
    沈念sama閱讀 223,002評論 6 519
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異矢空,居然都是意外死亡航罗,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,357評論 3 400
  • 文/潘曉璐 我一進店門屁药,熙熙樓的掌柜王于貴愁眉苦臉地迎上來粥血,“玉大人,你說我怎么就攤上這事「纯鳎” “怎么了趾娃?”我有些...
    開封第一講書人閱讀 169,787評論 0 365
  • 文/不壞的土叔 我叫張陵,是天一觀的道長缔御。 經(jīng)常有香客問我抬闷,道長,這世上最難降的妖魔是什么耕突? 我笑而不...
    開封第一講書人閱讀 60,237評論 1 300
  • 正文 為了忘掉前任笤成,我火速辦了婚禮,結(jié)果婚禮上眷茁,老公的妹妹穿的比我還像新娘炕泳。我一直安慰自己,他們只是感情好上祈,可當(dāng)我...
    茶點故事閱讀 69,237評論 6 398
  • 文/花漫 我一把揭開白布培遵。 她就那樣靜靜地躺著,像睡著了一般登刺。 火紅的嫁衣襯著肌膚如雪荤懂。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,821評論 1 314
  • 那天塘砸,我揣著相機與錄音节仿,去河邊找鬼。 笑死掉蔬,一個胖子當(dāng)著我的面吹牛廊宪,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播女轿,決...
    沈念sama閱讀 41,236評論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼箭启,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蛉迹?” 一聲冷哼從身側(cè)響起傅寡,我...
    開封第一講書人閱讀 40,196評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎北救,沒想到半個月后荐操,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,716評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡珍策,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,794評論 3 343
  • 正文 我和宋清朗相戀三年托启,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片攘宙。...
    茶點故事閱讀 40,928評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡屯耸,死狀恐怖拐迁,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情疗绣,我是刑警寧澤线召,帶...
    沈念sama閱讀 36,583評論 5 351
  • 正文 年R本政府宣布,位于F島的核電站多矮,受9級特大地震影響灶搜,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜工窍,卻給世界環(huán)境...
    茶點故事閱讀 42,264評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望前酿。 院中可真熱鬧患雏,春花似錦、人聲如沸罢维。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,755評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽肺孵。三九已至匀借,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間平窘,已是汗流浹背吓肋。 一陣腳步聲響...
    開封第一講書人閱讀 33,869評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留瑰艘,地道東北人是鬼。 一個月前我還...
    沈念sama閱讀 49,378評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像紫新,于是被迫代替她去往敵國和親均蜜。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,937評論 2 361

推薦閱讀更多精彩內(nèi)容