100. Same Tree

Posted optor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了100. Same Tree相关的知识,希望对你有一定的参考价值。

原题链接:https://leetcode.com/problems/same-tree/description/
这道题目本身超级简单,解法就是递归遍历二叉树即可!

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == q) {
            return true;
        }
        
        if (p == null || q == null) {
            return false;
        }
        
        if (p.val == q.val) {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }

        return false;
    }

以上是关于100. Same Tree的主要内容,如果未能解决你的问题,请参考以下文章

100. Same Tree

100. Same Tree(LeetCode)

100. Same Tree(Tree)

LeetCode100. Same Tree-相同树

[LeetCode] 100. Same Tree ☆(两个二叉树是否相同)

Leetcode[100]-Same Tree