[leet code 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.

Hide Tags

Tree Depth-first Search

2 思路

好久没做树的题目了,选了一道最简单的树的题目。看别人答案代码思路就很简单了,自己想,想半天思路并不是很好,各种漏洞。

参考:

https://leetcode.com/discuss/15083/five-line-java-solution-with-recursion

https://leetcode.com/discuss/3470/seeking-for-better-solution

3 代码

    public boolean isSameTree(TreeNode p,TreeNode q){
        if (p == null || q == null) return (p == q);
        return (p.val == q.val) && isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }
时间: 2024-10-28 11:41:32

[leet code 100] same tree的相关文章

#Leet Code# Unique Tree

语言:Python 描述:使用递归实现 1 class Solution: 2 # @return an integer 3 def numTrees(self, n): 4 if n == 0: 5 return 0 6 elif n == 1: 7 return 1 8 else: 9 part_1 = self.numTrees(n-1) * 2 10 part_2 = 0 11 12 for i in range(1,n-1): 13 part_left = self.numTrees(

#Leet Code# Same Tree

语言:Python 描述:使用递归实现 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self.left = None 6 # self.right = None 7 8 class Solution: 9 # @param p, a tree node 10 # @param q, a tree node 11 # @return

#Leet Code# Convert Sorted Array to Binary Search Tree

描述:递归 代码: 1 class Solution: 2 # @param num, a list of integers 3 # @return a tree node 4 def sortedArrayToBST(self, num): 5 if len(num) == 0: 6 return None 7 8 mid_index = len(num) / 2 9 10 tmp_tree = TreeNode(num[mid_index]) 11 tmp_tree.left = self.

【Leet Code】Palindrome Number

Palindrome Number Total Accepted: 19369 Total Submissions: 66673My Submissions Determine whether an integer is a palindrome. Do this without extra space. 判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的[Leet Code]Reverse Integer--"%"你真的懂吗? 不过这里要考虑翻转后,数值

#Leet Code# Populating Next Right Pointers in Each Node II

描述:注意需要先self.connect(right)再self.connect(left),否则会有case通不过,原因是左边递归执行时依赖与右边的next已经建立,而先执行connect(left)的话右边还没有完成关系的建立. 代码: 1 class Solution: 2 # @param root, a tree node 3 # @return nothing 4 def doSth(self, nextNode, conNode): 5 while nextNode is not

#Leet Code# Root to leaf

语言:Python 描述:使用递归实现 1 def getList(self, node): 2 if node is None: 3 return [] 4 5 if node.left is None and node.right is None: 6 return [[node.val]] 7 8 result = [] 9 for item in self.getList(node.left): 10 result.append([node.val] + item) 11 12 for

#Leet Code# Evaluate Reverse Polish Notation

描述:计算逆波兰表达法的结果 Sample: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6 使用stack实现: 1 def is_op

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