Same Tree -- leetcode

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.

基本思路:

进行递归操作,判断两个树是否相等。

1. 两树若都为空树,则相等。

2. 两树都不空;根结点的值相等;左子树相等;右子树相等。则该树相等。

在leetcode上实际执行时间为2ms。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        if (!p && !q) return true;
        return p && q &&
               p->val == q->val &&
               isSameTree(p->left, q->left) &&
               isSameTree(p->right, q->right);
    }
};
时间: 2024-09-27 01:41:18

Same Tree -- leetcode的相关文章

Minimum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 题解: 递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth. 递归解法: 1     public 

Symmetric Tree leetcode java

题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it both

Same Tree leetcode 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. 题解: 递归判断左右是否相等. 代码如下: 1     public boolean isSameTree(TreeNode

Maximum Depth of Binary Tree leetcode java

题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 题解: 递归解法: 1     public int maxDepth(TreeNode root) {2         if(root==null)3         

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

Balanced Binary Tree leetcode java

题目: 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 subtrees of every node never differ by more than 1. 题解: 采用递归的方法,要记录depth用来比较. 代码如下:

Recover Binary Search Tree leetcode java

题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space solution? 题解: 解决方法是利用中序遍历找顺序不对的两个点

Validate Binary Search Tree leetcode java

题目: 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

Maximum Depth of Binary Tree -- leetcode

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 基本思路: 深度优先遍历. 在leetcode上实行执行时间为18ms. /** * Definition for a binary tree node. * struct Tre