zoukankan      html  css  js  c++  java
  • [Leetcode] Same Tree

    Same Tree 题解

    原创文章,拒绝转载

    题目来源:https://leetcode.com/problems/same-tree/description/


    Description

    Given two binary trees, write a function to check if they are the same or not.

    Two binary trees are considered the same if they are structurally identical and the nodes have the same value.

    Example

    Example 1:

    
    Input:     1         1
              /        / 
             2   3     2   3
    
            [1,2,3],   [1,2,3]
    
    Output: true
    
    

    Example 2:

    
    Input:     1         1
              /           
             2             2
    
            [1,2],     [1,null,2]
    
    Output: false
    
    

    Example 3:

    
    Input:     1         1
              /        / 
             2   1     1   2
    
            [1,2,1],   [1,1,2]
    
    Output: false
    
    

    Solution

    
    class Solution {
    private:
        bool isSameNode(TreeNode* node1, TreeNode* node2) {
            if (node1 == NULL && node2 == NULL)
                return true;
            if (node1 != NULL && node2 != NULL) {
                return (node1 -> val == node2 -> val) &&
                        isSameNode(node1 -> left, node2 -> left) &&
                        isSameNode(node1 -> right, node2 -> right);
            } else {
                return false;
            }
        }
    public:
        bool isSameTree(TreeNode* node1, TreeNode* node2) {
            return isSameNode(node1, node2);
        }
    };
    
    
    

    解题描述

    这道题是要判断给出的两棵二叉树是否完全相等,也就是判断两棵树是不是结构完全相同而且每个节点的数字是否完全一样。一开始我想到的还是简单的递归做法,也就是上面给出来的。不过跑出来的时间确实还是比较长。

    AC之后看了讨论区,发现还是可以用非递归的方式来解决的。主要是使用栈来模拟递归调用,跑出来的时间也确实缩短了一些:

    
    class Solution {
    public:
        bool isSameTree(TreeNode* node1, TreeNode* node2) {
            if (node1 == NULL && node2 == NULL)
                return true;
            if (node1 != NULL && node2 != NULL) {
                stack<TreeNode*> stack1;
                stack<TreeNode*> stack2;
                stack1.push(node1);
                stack2.push(node2);
                TreeNode *n1, *n2;
                while (!stack1.empty() && !stack2.empty()) {
                    n1 = stack1.top();
                    stack1.pop();
                    n2 = stack2.top();
                    stack2.pop();
    
                    if (n1 -> val != n2 -> val) return false;
    
                    if (n1 -> left != NULL)
                        stack1.push(n1 -> left);
                    if (n2 -> left != NULL)
                        stack2.push(n2 -> left);
                    if (stack1.size() != stack2.size())
                        return false;
    
                    if (n1 -> right != NULL)
                        stack1.push(n1 -> right);
                    if (n2 -> right != NULL)
                        stack2.push(n2 -> right);
                    if (stack1.size() != stack2.size())
                        return false;
                }
                return stack1.size() == stack2.size();
            } else {
                return false;
            }
        }
    };
    
    
  • 相关阅读:
    python网络编程学习笔记(3):socket网络服务器
    Python编码爬坑指南
    ROT13 维基百科,自由的百科全书
    ZODB + Traversal Wiki Tutorial¶
    ZODB programming guide¶
    利用新浪api获取ip归属地 QtSharp 博客园
    用python做了个桌球瞄准器
    Python运维工具介绍1–fabric
    python httplib2 使用代理出错
    第四回 基类中的修饰符,应该根据你对架构的理解去定义它们,没有绝对的
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8330417.html
Copyright © 2011-2022 走看看