【LeetCode从零单刷】Same Tree

没错我就是伍声2009的粉丝,从今天起,模仿《从零单排》系列,菜鸡单刷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.

解答:

验证两棵树是否相等。使用递归的思想,如果树节点的val相等,则去各自计算子树是否是相等的树。

注意递归终点:输入的两棵树全部都是空树时,输出 true. 所以我得到了这个:

/**
 * 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 == NULL && q == NULL)
            return true;
        else if(p->val == q->val)
            return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
        else
            return false;
    }
};

好吧没有通过……当输入空树,与一棵非空树比较时,空树根本无法取到 val 值。所以要将输入空树的情况首先(如果放到以后的分支中,还是会遇到空树求值的情况)全部排除。得到:

/**
 * 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 == NULL && q == NULL)
            return true;
        else if((p==NULL && q!=NULL) || (q==NULL && p!=NULL) || (p->val != q->val))
            return false;
        else if(p->val == q->val)
            return (isSameTree(p->left, q->left) && isSameTree(p->right, q->right));
    }
};
时间: 2024-10-12 11:39:40

【LeetCode从零单刷】Same Tree的相关文章

【LeetCode从零单刷】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从零单刷]Binary

【LeetCode从零单刷】Maximum Depth of Binary Tree

没错我就是伍声2009的粉丝,从今天起,模仿<从零单排>系列,菜鸡单刷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从零单刷】Binary Tree Preorder Traversal

题目: Given a binary tree, return the preorder traversal of its nodes' values. Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 解答: 这题难在使用迭代而不是递归.思考一下,先序遍历过程中是不是处理根之后,根的左子树变成新的根,还有子树

【LeetCode从零单刷】Populating Next Right Pointers in Each Node

题目: Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; } Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially

【LeetCode从零单刷】Longest Increasing Subsequence

题目: Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more

【LeetCode从零单刷】Kth Smallest Element in a BST

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up: What if the BST is modified (insert/delete operations) often and you

【LeetCode从零单刷】Unique Binary Search Trees

题目: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ 3 2 1 1 3 2 / / \ 2 1 2 3 解答: 给出节点数,求可以构成的二叉搜索树的个数. 首先介绍什么是"二叉搜索树":

【LeetCode从零单刷】Excel Sheet Column Number

题目: Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 解答: 怎么说,类似26进制数一样.乍看之下挺简单.于是写下: class Solution { public: int titleToNumber(stri

【LeetCode从零单刷】Bulb Switcher

题目: There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off if it's on). For the nth round, you only toggl