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;
            }
        }
    };
    
    
  • 相关阅读:
    SpringCloud微服务初步认识
    SpringCloud-Hystrix:服务熔断与降级
    List接口下重要集合源码分析
    高频面试题:手写一个LRU
    Java基础面试题面经整理(持续更新)
    Redis高可用之主从复制
    Redis过期键删除和内存淘汰
    Redis持久化(RDB与AOF)
    了解Redis事务
    Redis入门与安装
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8330417.html
Copyright © 2011-2022 走看看