LeetCode 100. SameTree
题意
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.
给定两棵二叉树,需要判断他们是否相等,相等的定义是结构相同且对应的值相等。
思路
根据不同类型,对二叉树的节点进行标记。例如将空节点(父节点指针为空所抽象出来的节点)记为 0 ,仅有左子树的节点记为 1 ,仅有右子树的节点记为 2 ,叶子节点记为 3 ,同时有左右子树的节点记为 4 ,使用相同的遍历算法分别遍历两个二叉树,得到节点类型序列和节点值的序列,当且仅当节点类型序列和节点值序列相等时两二叉树相等。
代码
|
|