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;
            }
        }
    };
    
    
  • 相关阅读:
    “孤立”用户
    MongoDB 维护Replica Set
    Design7:数据删除设计
    abap取中值的函数
    REPLACE...IN.....WITH.... 的使用
    ABAP中RETURN与EXIT语句的区别
    在一个程序中调用另一个程序并且传输数据到选择屏幕执行这个程序
    Extract Datasets
    事件
    计算字符串长度的实例
  • 原文地址:https://www.cnblogs.com/yanhewu/p/8330417.html
Copyright © 2011-2022 走看看