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.
题意:
给定两个二叉树,编写一个函数检查它们是否相等。二叉树相同要求他们的树结构相同,节点中的值也相同。
思路:
方法一:
先遍历的变形,同时先序遍历两个树结构,非递归实现:
1 | class Solution { |
方法二:
先遍历的变形,同时先序遍历两个树结构,递归实现:
1 | class Solution { |