zoukankan      html  css  js  c++  java
  • [Leetcode] 100. 相同的树

    题目链接 : https://leetcode-cn.com/problems/same-tree/

    题目描述:

    给定两个二叉树,编写一个函数来检验它们是否相同。

    如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

    示例:

    示例 1:

    输入:       1         1
              /        / 
             2   3     2   3
            [1,2,3],   [1,2,3]
    输出: true        
    

    示例 2:

    输入:      1          1
              /           
             2             2    
       	 [1,2],     [1,null,2]
    输出: false   	 
    

    示例 3:

    输入:       1         1
              /        / 
             2   1     1   2  
            [1,2,1],   [1,1,2]
    输出: false
    

    思路:

    直接看代码!

    思路一:递归

    思路二:迭代,看先序遍历是否一样

    代码:

    思路一:递归

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
            if not p and not q: return True
            if p and q and p.val == q.val :
                return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)     
            return False
    

    java

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if (p == null && q == null) return true;
            if (p != null && q != null && p.val == q.val) return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
            else return false;
        }
    }
    

    思路二:

    # Definition for a binary tree node.
    # class TreeNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution:
        def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
            stack = [(q, p)]
            while stack:
                a, b = stack.pop()
                if not a and not b:
                    continue
                if a and b and a.val == b.val:
                    stack.append((a.left, b.left))
                    stack.append((a.right,b.right))
                else:
                    return False
            return True
    

    java

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            Deque<TreeNode> stack1 = new LinkedList<>();
            Deque<TreeNode> stack2 = new LinkedList<>();
            stack1.push(p);
            stack2.push(q);
            while (!stack1.isEmpty() && !stack2.isEmpty()) {
                TreeNode a = stack1.pop();
                TreeNode b = stack2.pop();
                if (a == null && b == null) continue;
                if (a != null && b != null && a.val == b.val) {
                    stack1.push(a.left);
                    stack1.push(a.right);
                    stack2.push(b.left);
                    stack2.push(b.right);
                } else return false;
            }
            return stack1.isEmpty() && stack2.isEmpty();
        }
    }
    
  • 相关阅读:
    ansible自动化运维04
    ansible自动化运维03
    ansible自动化运维02
    ansible自动化运维01
    mysql innodb存储引擎和一些参数优化
    Mysql 数据库常用配置命令
    zabbix server3.4 使用mailx配置邮件报警
    32_redis cluster的核心原理分析:gossip通信、jedis smart定位、主备切换
    31_redis cluster的自动化slave迁移实现更强的高可用架构的部署方案
    30_redis cluster通过master水平扩容来支撑更高的读写吞吐+海量数据
  • 原文地址:https://www.cnblogs.com/powercai/p/11085304.html
Copyright © 2011-2022 走看看