java 100.相同的Tree.java

Posted

tags:

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

class Solution(object):
    def isSameTree(self, p, q):
        """
        :type p: TreeNode
        :type q: TreeNode
        :rtype: bool
        """
        
        if not p and not q:
            return True;
        elif not p or not q:
            return False;
        elif p.val != q.val:
            return False;
        else:
            return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right);
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null) return true;
        else if(p == null || q == null) return false;
        else if(p.val != q.val) return false;
        else return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

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

java 100.相同的Tree.java

java 100.相同的Tree.java

java 100.相同的Tree.java

java 100.相同的Tree.java

[LeetCode] 100. Same Tree Java

LeetCode Top 100 Liked Questions 98. Validate Binary Search Tree (Java版; Medium)