LeetCode Same Tree

Same Tree Total Accepted: 52651 Total Submissions: 125166 My Submissions Question Solution

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.

题意:

比较两棵二叉树是否完全一致

递归版解法:

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

54 / 54 test cases passed.

Status: Accepted

Runtime: 2 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;
        else
        {
            if(p->val != q->val)
                return false;
            else
            {
                queue<TreeNode*> lq;
                queue<TreeNode*> rq;
                lq.push(p);
                rq.push(q);
                while(!lq.empty() && !rq.empty())
                {
                    TreeNode* lfront = lq.front();
                    TreeNode* rfront = rq.front();
                    lq.pop();
                    rq.pop();
                    if(!lfront->left && !rfront->left)
                        ;// null
                    else if(!lfront->left && rfront->left)
                        return false;
                    else if(lfront->left && !rfront->left)
                        return false;
                    else
                    {
                        if(lfront->left->val != rfront->left->val)
                            return false;
                        else
                        {
                            lq.push(lfront->left);
                            rq.push(rfront->left);
                        }
                    }
                    if(!lfront->right && !rfront->right)
                        ;// null
                    else if(!lfront->right && rfront->right)
                        return false;
                    else if(lfront->right && !rfront->right)
                        return false;
                    else
                    {
                        if(lfront->right->val != rfront->right->val)
                            return false;
                        else
                        {
                            lq.push(lfront->right);
                            rq.push(rfront->right);
                        }
                    }
                }
                return true;
            }
        }
    }
};

54 / 54 test cases passed.

Status: Accepted

Runtime: 3 ms

时间: 2024-10-05 22:09:43

LeetCode Same Tree的相关文章

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#

[leetcode]Binary Tree Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal/ 题意:二叉树的层序遍历的实现. 解题思路:二叉树的层序遍历可以用bfs或者dfs来实现.这里使用的dfs实现,代码比较简洁.实际上,二叉树的先序遍历就是dfs实现.   比如一棵树如下: 1 /  \ 2       3 /    \    /   \ 4     5  6    7    二叉树的先序遍历为{1,2,4,5,3,6,7},可以看到这个遍

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between

[leetcode]Binary Tree Level Order Traversal II @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ 题意: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary

[LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz

LeetCode &quot;Binary Tree *-order Traversal&#39; by Iteration

Binary Tree *-order traversal by recursion is trivial. But their iteration version deserves a look: Pre-Order class Solution { vector<int> ret; public: vector<int> preorderTraversal(TreeNode *p) { if (!p) return ret; stack<TreeNode *> st

[leetcode]_Binary Tree Inorder Traversal

题目:二叉树的中序遍历. 思路:用递归来写中序遍历非常简单.但是题目直接挑衅说,----->"Recursive solution is trivial".好吧.谁怕谁小狗. 递归代码: 1 List<Integer> inOrder = new ArrayList<Integer>(); 2 3 public List<Integer> inorderTraversal(TreeNode root) { 4 inOrderT(root); 5

LeetCode: Binary Tree Maximum Path Sum [124]

[题目] Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / 2 3 Return 6. [题意] 给定一棵二叉树,找出其中路径和最大的路径,然会返回最大路径和. 本题中的路径不是从根节点到叶子节点这样的传统的路径,而是指的二叉树中任意两个节点之间的联通路径.

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig