• [leetcode-100-Same Tree]


    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.

    首先是递归版本:

    bool isSameTree(TreeNode* p, TreeNode* q)
        {//递归
            if (p == NULL && q == NULL) return true;
            if (p == NULL && q != NULL || p != NULL && q == NULL || q->val != p->val) return false;
    
            return isSameTree(p->left, q->left) && isSameTree(p->right,q->right);
        }

    运行效率:

    对比一下非递归版本:

    bool isSameTree2(TreeNode* p, TreeNode* q)
        {//先序非递归
            stack<TreeNode*>st1, st2;
            if (p != NULL)st1.push(p);
            if (q!= NULL)st2.push(q);
            TreeNode* ptemp;
            TreeNode* qtemp;
            while (!st1.empty() && !st2.empty())
            {
                ptemp = st1.top();
                qtemp = st2.top();
                if (ptemp->val != qtemp->val) return false;
                st1.pop();
                st2.pop();
                if (ptemp->right != NULL) st1.push(ptemp->right);
                if (qtemp->right != NULL) st2.push(qtemp->right);
                if (st1.size() != st2.size()) return false;//比较两个栈的大小 
    
                if (ptemp->left != NULL) st1.push(ptemp->left);
                if (qtemp->left != NULL) st2.push(qtemp->left);
                if (st1.size() != st2.size()) return false;//比较两个栈的大小 
            }
            return (st1.size() == st2.size());
        }

    运行效率:

    可见,非递归确实效率要高一些。

  • 相关阅读:
    猿辅导-去除数组中数量大于k的数
    OSI体系结构各层协议
    京东-学生合唱队分组
    146-LRUCache
    76-最长上升子序列
    无序数组中三个数字乘积最大值
    38-字符串的排列
    35-复杂链表的复制
    208-Implement Trie(Prefix Tree)
    69-求一个整数的平方根
  • 原文地址:https://www.cnblogs.com/hellowooorld/p/6442312.html
Copyright © 2020-2023  润新知