美文网首页
【2错1对-1】Same Tree

【2错1对-1】Same Tree

作者: 7ccc099f4608 | 来源:发表于2018-12-12 22:42 被阅读0次

https://leetcode.com/problems/same-tree/

日期 是否一次通过 comment
2018-12-12 20:37 非递归没思路,递归有部分思路 思维不够灵活
2019-01-13 16:06 忽视了何时返回true和false 思维不够灵活
image.png

(来源:https://leetcode.com/problems/same-tree/

递归

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null || q==null) {
            return p==q;
        }
        
        if(p.val != q.val) {
            return false;
        }
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        
    }
}

非递归

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        
        Queue<TreeNode[]> nodesQ = new LinkedList<>();
        nodesQ.offer(new TreeNode[]{p, q});
        while(!nodesQ.isEmpty()) {
            TreeNode[] nodes = nodesQ.poll();
            if(nodes[0] == null || nodes[1] == null) {
                if(!(nodes[0] == null && nodes[1] == null)) {
                    return false;
                }
                continue;
            } 
            if(nodes[0].val != nodes[1].val) {
                return false;
            }
            
            nodesQ.offer(new TreeNode[]{nodes[0].left, nodes[1].left});
            nodesQ.offer(new TreeNode[]{nodes[0].right, nodes[1].right});
            
        }
        return true;
    }
}

or

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        
        if(p == null || q == null) {
            return p == q;
        }
        
        Stack<TreeNode> pStack = new Stack<>();
        Stack<TreeNode> qStack = new Stack<>();
        
        pStack.push(p);
        qStack.push(q);
        
        while(!pStack.isEmpty() && !qStack.isEmpty()) {
            TreeNode nodeP = pStack.pop();
            TreeNode nodeQ = qStack.pop();
            if(nodeP.val != nodeQ.val) {
                return false;
            }
            
            if(nodeP.left != null) {
                pStack.push(nodeP.left);
            }
            if(nodeQ.left != null) {
                qStack.push(nodeQ.left);
            }
            if(pStack.size() != qStack.size()) {
                return false;
            }
            if(nodeP.right != null) {
                pStack.push(nodeP.right);
            }
            if(nodeQ.right != null) {
                qStack.push(nodeQ.right);
            }
            if(pStack.size() != qStack.size()) {
                return false;
            }
            
        }
        
        return pStack.isEmpty() && qStack.isEmpty();
    }
}

相关文章

  • 【2错1对-1】Same Tree

    https://leetcode.com/problems/same-tree/ (来源:https://leet...

  • 花与鸟

    1、But it is the same with man as with the tree. The more ...

  • 100 Same Tree

    title: Same Treetags:- same-tree- No.100- simple- tree- r...

  • Same Tree

    //100 Given two binary trees, write a function to check i...

  • Same Tree

    判断两个二进制树是否相同(树节点总数相同,树结构相同)通过迭代的方式解决:遍历:依次遍历树的节点的左右节点出口:树...

  • Same Tree

    题目描述Given two binary trees, write a function to check if ...

  • DFS-special

    Validate Binary Search Tree Same Tree (基础) 101.symmetric ...

  • Tree-E-100. Same Tree

    Tree-E-100. Same Tree 时间:20180301 分类:递归、前序遍历、比较大小 1.自己解法 ...

  • 100 Same Tree

    原题链接:Same Tree

  • LC100 Same Tree

    本题链接:Same Tree 本题标签:Tree, DFS 本题难度: 方案1: 时间复杂度: 空间复杂度:

网友评论

      本文标题:【2错1对-1】Same Tree

      本文链接:https://www.haomeiwen.com/subject/qtsmhqtx.html