Java [Leetcode 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 && q == null)
    		return true;
    	else if(p == null || q == null)
    		return false;
    	else
    		return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
}

  

时间: 2024-10-10 12:20:39

Java [Leetcode 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

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

LeetCode 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.  题目标题:Tree 这道题目给了我们两个二叉树,让我们判断这两个二叉树是否一摸一样.利用preOrder 来遍历tree, 对于每

小程序 - leetcode 100. Same Tree

https://leetcode.com/problems/same-tree/ /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool isSameTree(struct TreeNode* p, struct TreeNode* q) { if (p == NULL) retur

Java [Leetcode 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

Java [Leetcode 257]Binary Tree Paths

题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 解题思路: 使用递归法. 代码如下: /** * Definition for a binary tree node. * pu

Java [Leetcode 102]Binary Tree Level Order Traversal

题目描述: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

Java [Leetcode 144]Binary Tree Preorder Traversal

题目描述: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. 解题思路: 这个问题最简单的方法是使用递归,但是题目规定不能使用,得使用迭代的方法. 那么我们考虑使用栈来实现. 思路是每次遍历这节点,把该点的值放入list中,然后把该点的右孩子放入栈中,并将当前点设置为左孩子

leetcode 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. * struct TreeNode