Difficult:Easy
題目
Given the roots of two binary trees p and q, write a function to check if they are the same or not.
Two binary trees are considered the same if they are structurally identical, and the nodes have the same value.
翻譯
給定兩個二叉樹 p 和 q 的根,編寫一個函數來檢查它們是否相同。
如果兩個二叉樹在結構上相同並且節點具有相同的值,則它們被認為是相同的。
範例
Example 1:
1 | Input: p = [1,2,3], q = [1,2,3] |
Example 2:
1 | Input: p = [1,2], q = [1,null,2] |
Example 3:
1 | Input: p = [1,2,1], q = [1,1,2] |
解題思路
1.利用動態規劃
2.分析條件
Solution
1 | var isSameTree = function (p, q) { |