[LeetCode] 100. Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Subscribe to see which companies asked this question. Hide Tags Tree Depth-first Search 標準的深度優先搜索題目, 慢慢有點感覺了. 看到根樹有關的題目 第一個想到深度優先搜索(除了 題目要求 一層一層搜). 根據 樹的定義 The tree with no nodes is called the null or empty tree. 所以要注意看 兩個樹都為空時 return True line 11: 兩個樹都為空時 return True line 13: 一邊有node 一邊沒有 這絕對不是一樣的樹 line 15: 兩邊都有node 看看node裡的值 是否相等 line 17: 都判斷完以上條件了, 深入優先搜索 看看 左邊跟右邊的子樹 是否相等