Leetcode 5346 二叉树中的列表

题目描述:

题解:一开始处理的时候写了个dfs一直超时,其实先用bfs找到所有可能的起始点,对每个起始点dfs一下就可以了。

(纯dfs的话,如果树很深的时候,会一直先尝试一个分支到低端,很容易就超时了)

AC代码:

class Solution {
public:
    int dfs(ListNode* head,TreeNode* root)
    {
        if(head == NULL) return 1;
        if(head != NULL && root==NULL) return -1;
       // cout << head->val <<" " << root->val <<endl;
        if(head->val != root->val) return -1;
        if(dfs(head->next,root->right) == 1) return 1;
        if(dfs(head->next,root->left) == 1) return 1;
        return -1;
    }
    bool isSubPath(ListNode* head, TreeNode* root) {
        queue<TreeNode*> que;
        que.push(root);
        if(head == NULL) return true;
        if(head != NULL && root == NULL) return false;
        while(que.size())
        {
            TreeNode* now = que.front();
            que.pop();
            if(now->val == head->val)
            {
                if(dfs(head,now) == 1) return true;
            }
            if(now->left != NULL) que.push(now->left);
            if(now->right != NULL) que.push(now->right);
        }
        return false;

    }
};

原文地址:https://www.cnblogs.com/z1141000271/p/12394008.html

时间: 2024-08-11 14:29:50

Leetcode 5346 二叉树中的列表的相关文章

LeetCode 671. 二叉树中第二小的节点(Second Minimum Node In a Binary Tree) 9

671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法 2019/5/12Day 9 LeetCode671. Second Minimum Node In a B

图解leetcode —— 124. 二叉树中的最大路径和

前言: 每道题附带动态示意图,提供java.python两种语言答案,力求提供leetcode最优解. 描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例 1: 输入: [1,2,3] 1 / \ 2 3 输出: 6 示例 2: 输入: [-10,9,20,null,null,15,7] -10   / \  9  20    /  \   15   7 输出: 42 思路: java

[LeetCode] 124. 二叉树中的最大路径和

题目链接 : https://leetcode-cn.com/problems/binary-tree-maximum-path-sum/ 题目描述: 给定一个非空二叉树,返回其最大路径和. 本题中,路径被定义为一条从树中任意节点出发,达到任意节点的序列.该路径至少包含一个节点,且不一定经过根节点. 示例: 示例 1: 输入: [1,2,3] 1 / 2 3 输出: 6 示例 2: 输入: [-10,9,20,null,null,15,7] -10 / 9 20 / 15 7 输出: 42 思路

[leetcode]重建二叉树(先序和终须) 中序遍和后续

分割后长度相等,就是参数麻烦,p,先序的起始点, ib,ie 终须的结束和开始. 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 public TreeNode bu

Leetcode: Binary Tree Inorder Traversal(二叉树中序遍历)

题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 递归解法(C++): /** * Definition for binary tre

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

LeetCode OJ:Kth Smallest Element in a BST(二叉树中第k个最小的元素)

Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. 求二叉树中第k个最小的元素,中序遍历就可以了,具体代码和另一个Binary Tree Iterator差不多其实,这题由于把=写成了==调bug调了好久,细心细心啊啊

Leetcode 623.在二叉树中增加一行

在二叉树中增加一行 给定一个二叉树,根节点为第1层,深度为 1.在其第 d 层追加一行值为 v 的节点. 添加规则:给定一个深度值 d (正整数),针对深度为 d-1 层的每一非空节点 N,为 N 创建两个值为 v 的左子树和右子树. 将 N 原先的左子树,连接为新节点 v 的左子树:将 N 原先的右子树,连接为新节点 v 的右子树. 如果 d 的值为 1,深度 d - 1 不存在,则创建一个新的根节点 v,原先的整棵树将作为 v 的左子树. 示例 2: 注意: 输入的深度值 d 的范围是:[1

图解精选 TOP 面试题 002 | LeetCode 104. 二叉树的最大深度

该系列题目取自 LeetCode 精选 TOP 面试题列表:https://leetcode-cn.com/problemset/top/ 题目描述 原题链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明:叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20