[LeetCode]题解(python):100 Same Tree

题目来源



https://leetcode.com/problems/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.



题意分析



Input: twon binary tree

Output: equal?

Conditions:判断两个二叉树是不是相等。



题目思路



逐一遍历,看是否相等。



AC代码(Python)

 1 # Definition for a binary tree node.
 2 # class TreeNode(object):
 3 #     def __init__(self, x):
 4 #         self.val = x
 5 #         self.left = None
 6 #         self.right = None
 7
 8 class Solution(object):
 9     def isSameTree(self, p, q):
10         """
11         :type p: TreeNode
12         :type q: TreeNode
13         :rtype: bool
14         """
15         def dfs(p, q):
16             if p == None and q != None:
17                 return False
18             if p != None and q == None:
19                 return False
20             if p == None and q == None:
21                 return True
22             if p.val != q.val:
23                 return False
24             return dfs(p.left, q.left) and dfs(p.right, q.right)
25
26         return dfs(p,q)
27         
时间: 2024-10-27 18:23:25

[LeetCode]题解(python):100 Same Tree的相关文章

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

[LeetCode 题解]: Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

<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题解: Balanced Binary Tree

判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtree

LeetCode in Python 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,null,null,15,7], 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ]

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. 大意: 给出两个二叉树,写一个函数来检查两者是否相等. 所谓相等,是指他们结构相同且节点有同样的值. 思路: 这个思路还比较

LeetCode题解之Balanced Binary Tree

1.题目描述 2.问题分析 DFS. 3.代码 1 bool isBalanced(TreeNode* root) { 2 if (root == NULL) 3 return true; 4 5 return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); 6 7 } 8 9 int

[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

全部最新的题解可以在 我的 github 上找,欢迎 star 和 watch ~ 更新中~~ 说明 这个系列的题解包括用 C++/Java/Python 写的 leetcode 上的算法题目,和 Sql 写的 leetcode 上的数据库题目. 有些题目虽然 AC 了却还没写分析,所以这次就开坑来完成. 链接: 我的 github Leetcode Algorithms Problems Leetcode Database Problems CSDN 题解索引 001.Two_Sum (Med

[LeetCode]题解(python):031-Next Permutation

题目来源 https://leetcode.com/problems/next-permutation/ Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible