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;
            }
        }
    };
    
    
  • 相关阅读:
    spring mybatis 关于 basepackage 和 mapperLocations 的通配符匹配实例
    nginx 隐藏版本信息
    nginx 虚拟主机配置
    分析解决 spring quartz 中出现的执行两次问题
    nginx 安装配置和常用命令
    JMX 远程监控 Linux tomcat 功能实现
    jvm 类加载机制
    jvm 类文件结构学习
    转 MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
    MySQL提示:The server quit without updating PID file问题的解决办法
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8330417.html
Copyright © 2011-2022 走看看