zoukankan      html  css  js  c++  java
  • Leetcode 1367 二叉树中的列表 DFS

     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);
        }
  • 相关阅读:
    基本数据结构:链表(list)
    字符串函数
    TCHAR
    开源库链接
    视频 链接
    tabbar
    加密
    安全类链接,https
    资本
    审核 -链接 - 发布证书
  • 原文地址:https://www.cnblogs.com/niuyourou/p/13252119.html
Copyright © 2011-2022 走看看