10.Binary Tree(二叉樹)

定義

at most two children node. 最多有兩個子節(jié)點的樹勿侯。


image.png

基本知識點

1.LinkedList可以看成是Binary Tree的變種形式缨硝。
2.DFS (Depth-first search)
a. Pre-order: parent before children
b. In-order: parent in the middle of children
c. Post-order: parent after children
這里的pre、in纫普、post詞根就是root節(jié)點與其left预侯、right子節(jié)點相比的優(yōu)先順序。
3.BFS (Breadth-first search)
a. Level by level

(1)pre-order
parent->left->right:先打印自己 再打印左右子節(jié)點
題目:
Implement an iterative, in-order traversal of a given binary tree, return the list of keys of each node in the tree as it is in-order traversed.
Examples
5
/
3 8
/ \
1 4 11
In-order traversal is [1, 3, 4, 5, 8, 11]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.

/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
  public List<Integer> inOrder(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    if(root == null)return list;
    inOrder(root, list);
    return list;
  }
  
  private void inOrder(TreeNode root, List<Integer> list){
      if(root == null){
        return;
      }
      inOrder(root.left, list);
      list.add(root.key);
      inOrder(root.right, list);
  }
}

(2)in-order
left->parent->right:先打印左子節(jié)點 再打印自己 最后打印右子節(jié)點
題目:
Implement an iterative, pre-order traversal of a given binary tree, return the list of keys of each node in the tree as it is pre-order traversed.
Examples
5
/
3 8
/ \
1 4 11
Pre-order traversal is [5, 3, 1, 4, 8, 11]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.

/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
  public List<Integer> preOrder(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    preOrder(root, list);
    return list;
  }
  
  private void preOrder(TreeNode root, List<Integer> list){
    if(root == null) return;
    list.add(root.key);
    preOrder(root.left, list);
    preOrder(root.right, list);
  }
}

(3)post-order
left->right->parent:先打印左子節(jié)點 再打印右子節(jié)點 最后打印自己
題目:
Implement an iterative, post-order traversal of a given binary tree, return the list of keys of each node in the tree as it is post-order traversed.
Examples
5
/
3 8
/ \
1 4 11
Post-order traversal is [1, 4, 3, 11, 8, 5]
Corner Cases
What if the given binary tree is null? Return an empty list in this case.

/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
  public List<Integer> postOrder(TreeNode root) {
    List<Integer> list = new ArrayList<>();
    postOrder(root, list);
    return list;
  }
  
  private void postOrder(TreeNode root, List<Integer> list){
    if(root == null) return;
    postOrder(root.left, list);
    postOrder(root.right, list);
    list.add(root.key);
  }
}

基本概念

  • Height of binary tree:The distance between the root wirh the deepest leaf node.

  • Level of binary tree: ...

  • Balanced binary tree: is commonly defined as a binary tree in which the depth(also known as height) of the left and right subtrees of every node differ by 1 or less.

    1. foreach of the nodes in this binary tree
    2. satisfy: the height of leftsubtree, rightsubtree at most diff by 1.


      image.png
  • complete binary tree: is a binary tree in which every level, except possibly the last, is completely filled,and all nodes are as far left as possible.


    image.png
  • Full binary tree:A full binary tree(somtimes proper binary tree or 2-tree) is a tree in which every node other than the leaves has two children.


    image.png
  • Binary Search Tree: for every single node in the tree, the values in its left subtree ars all smaller than(or equal to) its value, and the values in its right subtree are all larger than (or equal to) its value.


    image.png

習題

1.Height of Binary Tree
/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
   public int findHeight(TreeNode root) {
     if(root==null)return 0;
     int leftHeight = findHeight(root.left);
     int rightHeight = findHeight(root.right);
     return Math.max(leftHeight, rightHeight)+1;
   }
}

Time Complexity:
- Each function call (this level): O(1)
- There are in total O(n) function calls
- Summary: O(n)

Space Complexity:
- O(height), height= O(lgn)~O(n)
- Worst case:O(n), like linked list
- On average: O(lgn)

2.Count Node
/**
 * public class TreeNode {
 *   public int key;
 *   public TreeNode left;
 *   public TreeNode right;
 *   public TreeNode(int key) {
 *     this.key = key;
 *   }
 * }
 */
public class Solution {
   public int countNodes(TreeNode root) {
     if(root==null)return 0;
     int leftcount = countNodes(root.left);
     int rightcount = countNodes(root.right);
     return rightcount +rightcount +1;
   }
}

Time Complexity:
- Each function call (this level): O(1)
- There are in total O(n) function calls
- Summary: O(n)

Space Complexity:
- O(height), height= O(lgn)~O(n)
- Worst case:O(n), like linked list
- On average: O(lgn)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末毛嫉,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子妇菱,更是在濱河造成了極大的恐慌,老刑警劉巖暴区,帶你破解...
    沈念sama閱讀 206,602評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件闯团,死亡現(xiàn)場離奇詭異,居然都是意外死亡仙粱,警方通過查閱死者的電腦和手機房交,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評論 2 382
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來伐割,“玉大人候味,你說我怎么就攤上這事「粜模” “怎么了白群?”我有些...
    開封第一講書人閱讀 152,878評論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長硬霍。 經(jīng)常有香客問我帜慢,道長,這世上最難降的妖魔是什么唯卖? 我笑而不...
    開封第一講書人閱讀 55,306評論 1 279
  • 正文 為了忘掉前任粱玲,我火速辦了婚禮,結(jié)果婚禮上拜轨,老公的妹妹穿的比我還像新娘抽减。我一直安慰自己,他們只是感情好橄碾,可當我...
    茶點故事閱讀 64,330評論 5 373
  • 文/花漫 我一把揭開白布卵沉。 她就那樣靜靜地躺著,像睡著了一般堪嫂。 火紅的嫁衣襯著肌膚如雪偎箫。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,071評論 1 285
  • 那天皆串,我揣著相機與錄音淹办,去河邊找鬼。 笑死恶复,一個胖子當著我的面吹牛怜森,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播副硅,決...
    沈念sama閱讀 38,382評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼恐疲!你這毒婦竟也來了腊满?” 一聲冷哼從身側(cè)響起培己,我...
    開封第一講書人閱讀 37,006評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎省咨,沒想到半個月后肃弟,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,512評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡零蓉,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,965評論 2 325
  • 正文 我和宋清朗相戀三年笤受,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片敌蜂。...
    茶點故事閱讀 38,094評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡箩兽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出紊册,到底是詐尸還是另有隱情比肄,我是刑警寧澤,帶...
    沈念sama閱讀 33,732評論 4 323
  • 正文 年R本政府宣布囊陡,位于F島的核電站芳绩,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏撞反。R本人自食惡果不足惜妥色,卻給世界環(huán)境...
    茶點故事閱讀 39,283評論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望遏片。 院中可真熱鬧嘹害,春花似錦、人聲如沸吮便。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,286評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽髓需。三九已至许师,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背微渠。 一陣腳步聲響...
    開封第一講書人閱讀 31,512評論 1 262
  • 我被黑心中介騙來泰國打工搭幻, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人逞盆。 一個月前我還...
    沈念sama閱讀 45,536評論 2 354
  • 正文 我出身青樓檀蹋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親云芦。 傳聞我的和親對象是個殘疾皇子俯逾,可洞房花燭夜當晚...
    茶點故事閱讀 42,828評論 2 345

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