[leedcode 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.

/**
 * 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) return q==null;
        if(q==null) return false;
        if(p.val!=q.val)return false;
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}
时间: 2025-01-18 08:30:34

[leedcode 100] Same Tree的相关文章

leetCode 100. Same Tree 树

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. 题目大意: 判断两个二叉树是否完全相同.包括他们节点的内容. 代码如下:(递归版) /**  * De

100.Same Tree(Swift待解)

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. 思路:刚刚学习了二叉树,想到可能要分别遍历再比较,但对此题具体的实现还没有经验和具体代码结构. 参考代码: /* https://l

100. Same Tree(C++)

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. Subscribe to see which companies asked this questio

<LeetCode OJ> 100. Same Tree

100. Same Tree Total Accepted: 100129 Total Submissions: 236623 Difficulty: Easy 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 t

LeetCode Javascript实现 100. Same Tree 171. Excel Sheet Column Number

100. Same Tree /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} p * @param {TreeNode} q * @return {boolean} */ var isSameTree = function(p, q) { if(p

[LeetCode] NO. 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. [题目解析]  二叉树的问题,转化成当前节点与左子树,右子树问题.注意一些临界条件的判断. /** * Definitio

100. Same Tree

1. 问题描述 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. Subscribe to see which companies asked this question Tags:

[leedcode 107] Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order traver

[LeetCode] 100. Same Tree Java

题目: 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.. 题意及分析:给出两棵树,判断两棵树是否完全相同,结构和值都相同.有两种方法,一种是遍历树,对每次的节点做判断,比较复杂:另