LeetCode (65):Same tree

Total Accepted: 83663 Total Submissions: 200541 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 the same value.

首先想到的是递归法,判断过程为依次遍历每一个点如果有:

  1. 如果都为null或是都不为null且值相等返回true
  2. 一个为Null另一个不为Null返回false
  3. 两个都不为null但值不相等返回false

代码如下:

class TreeNode{
       int val;
            TreeNode left;
           TreeNode right;
            TreeNode(int x) { val = x; }
}
public class Solution {
     public static boolean isSameTree(TreeNode p, TreeNode q) {
         if(p==null&&q==null)     return true;  //同时到达叶子节点
         else  if(p==null||q==null) return false;   //不同时到达叶子则不相同
         if(p.val!=q.val) return false;  //节点值不同则不相同
         else return isSameTree(p.left, q.left)&&isSameTree(p.right, q.right); //递归

     }
           //测试
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        TreeNode t1=new TreeNode(1);
        TreeNode nodeB=new TreeNode(2);
        t1.left=nodeB;

        TreeNode t2=new TreeNode(1);
        TreeNode nodeB1=new TreeNode(2);
        t2.left=nodeB1;
        System.out.println(isSameTree(t1,t2));
    }

}
时间: 2024-10-15 15:52:12

LeetCode (65):Same tree的相关文章

LeetCode OJ - Binary Tree Level Order Traversal 1 && 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 && 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 OJ - Binary Tree Maximum Path

这道题需要注意的地方有以下一些: 1. 求从子树中的某节点到当前节点的最大路径不能采用递归方法,因为这个部分会被反复的调用,如果用递归,会使得之前已经计算过的节点被重复计算,使得时间复杂度特别高: 2. 树中有节点的值是负数的. 下面是AC代码.(我发现AC并不代表代码真的完全正确!!) 1 /** 2 * Given a binary tree, find the maximum path sum. 3 * The path may start and end at any node in t

[LeetCode OJ] Symmetric Tree

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 recu

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