Leetcode (5) 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.

简而言之,判断两棵二叉树是否相等,这里可以通过递归去判断,题目比较简单明了直接上代码吧。需要注意的是在判断左子树不相等的时候就可以直接return false了,而无需等左右子树都遍历完再返回结果。这样可以节省时间。运行时间为3 ms.

/**
 * Definition for binary tree
 * 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;
        else if ( p && !q )
            return false;
        else if ( !p && q )
            return false;

        if (p->val != q->val)
            return false;
        else
        {
            if (!isSameTree(p->left, q->left))
                return false;
            if (!isSameTree(p->right, q->right))
                return false;
        }
        return true;
    }
};
时间: 2024-08-04 04:19:15

Leetcode (5) Same Tree的相关文章

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

LeetCode:Symmetric Tree - 判断二叉树是否对称

1.题目名称 Symmetric Tree(判断二叉树是否对称) 2.题目地址 https://leetcode.com/problems/symmetric-tree/ 3.题目内容 英文:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 中文:给定一颗二叉树,检查它是否与自己的镜像是同一棵树(即围绕根节点对称). 4.解题方法 本题与题目"Same Tr

[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. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

LeetCode:Same Tree - 判断两颗树是否相等

1.题目名称 Same Tree(判断两棵树是否相等) 2.题目地址 https://leetcode.com/problems/same-tree/ 3.题目内容 英文: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 h

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

LeetCode OJ - Symmetric Tree &amp;&amp; Same Tree

这两道题,大同小异. 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同. 下面是AC代码: 1 /** 2 * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 3 * @param root 4 * @return 5 */ 6 public boolean isSymmetricRecursively(TreeNode root){ 7

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

[LeetCode 题解]: 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]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>