码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 1367 二叉树中的列表 DFS

时间:2020-07-05 23:20:05      阅读:68      评论:0      收藏:0      [点我收藏+]

标签:mamicode   class   new   date   卡尔   疑问   image   null   desc   

技术图片

 

 public final boolean isSubPath(ListNode head, TreeNode root) {
        if (root == null) {
            return false;
        }
        Stack<TreeNode> stack = new Stack<TreeNode>();
        stack.push(root);
        while (stack.size() > 0) {
            TreeNode node = stack.pop();
            if (isSubPathDP(head, node)) {
                return true;
            }
            if (node.left != null) {
                stack.push(node.left);
            }
            if (node.right != null) {
                stack.push(node.right);
            }
        }
        return false;
    }

    /**
     * @Author Niuxy
     * @Date 2020/7/5 9:20 下午
     * @Description 暴力解法毫无疑问要求链表所有节点与二叉树所有节点的笛卡尔积
     * G(l,t) 为 链表中的 l 节点是否作为二叉树中的 t 节点,后续元素是否全部匹配
     */
    public final boolean isSubPathDP(ListNode l, TreeNode t) {
        if (l == null) {
            return true;
        }
        if (t == null || l.val != t.val) {
            return false;
        }
        return isSubPathDP(l.next, t.left) || isSubPathDP(l.next, t.right);
    }

 

Leetcode 1367 二叉树中的列表 DFS

标签:mamicode   class   new   date   卡尔   疑问   image   null   desc   

原文地址:https://www.cnblogs.com/niuyourou/p/13252119.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!