100. Same Tree(Tree)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    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-29 19:10:59

100. Same Tree(Tree)的相关文章

LeetCode详细分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

LeetCode(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. 思路:  DFS AC代码: 1.递归 1

100. Same Tree (Tree;DFS)

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 || 100、Same Tree

problem: 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 题意:检查两棵 二叉树是否相同 ,二叉树相同定

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. 分析: 题目要求判断两棵树是否相同,首先判断根节点是否相同,如果根节点相同,分别判断左.右子树是否相同. 代码如下: /** * D

LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 2.Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 这里两道题目.是连在一起的两题.给你一个排好序(升序)的数组或者链表,将它们

LeetCode具体分析 :: Recover Binary Search Tree [Tree]

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? confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ. 这里

Jan 12 - Lowest Common Ancestor of a Binary Search Tree; Tree; BST; Recursion;

/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q)

110. Balanced Binary Tree (Tree; DFS)

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. struct TreeNode { int val; TreeNode