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).
  • If the current direction is right then move to the right child of the current node otherwise move to the left child.
  • Change the direction from right to left or right to left.
  • Repeat the second and third step until you can‘t move in the tree.

Zigzag length is defined as the number of nodes visited - 1. (A single node has a length of 0).

Return the longest ZigZag path contained in that tree.

Example 1:

Input: root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
Output: 3
Explanation: Longest ZigZag path in blue nodes (right -> left -> right).

Example 2:

Input: root = [1,1,1,null,1,null,null,1,1,null,1]
Output: 4
Explanation: Longest ZigZag path in blue nodes (left -> right -> left -> right).

Example 3:

Input: root = [1]
Output: 0

Constraints:

  • Each tree has at most 50000 nodes..
  • Each node‘s value is between [1, 100].

问题

力扣

给你一棵以 root 为根的二叉树,二叉树中的交错路径定义如下:

  • 选择二叉树中 任意 节点和一个方向(左或者右)。
  • 如果前进方向为右,那么移动到当前节点的的右子节点,否则移动到它的左子节点。
  • 改变前进方向:左变右或者右变左。
  • 重复第二步和第三步,直到你在树中无法继续移动。

交错路径的长度定义为:访问过的节点数目 - 1(单个节点的路径长度为 0 )。

请你返回给定树中最长 交错路径 的长度。

示例 1:

输入:root = [1,null,1,1,1,null,null,1,1,null,1,null,null,null,1,null,1]
输出:3
解释:蓝色节点为树中最长交错路径(右 -> 左 -> 右)。

示例 2:

输入:root = [1,1,1,null,1,null,null,1,1,null,1]
输出:4
解释:蓝色节点为树中最长交错路径(左 -> 右 -> 左 -> 右)。

示例 3:

输入:root = [1]
输出:0

提示:

  • 每棵树最多有 50000 个节点。
  • 每个节点的值在 [1, 100] 之间。

思路

DFS

递归时记录前面的节点是左节点还是右节点,以及深度。
Python3代码
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def longestZigZag(self, root: TreeNode) -> int:
        if root == None:
            return 0
        self.max_ = 0
        self.dfs(root, 0, 0)
        return self.max_

    def dfs(self, root, prev, depth):
        self.max_ = max(depth, self.max_)

        if root.left:
            # left->left
            if prev == 0:
                self.dfs(root.left, 0, 1)
            # left->right
            else:
                self.dfs(root.left, 0, depth + 1)
        if root.right:
            # right->right
            if prev == 1:
                self.dfs(root.right, 1, 1)
            # right->left
            else:
                self.dfs(root.right, 1, depth + 1)

代码地址

GitHub链接

原文地址:https://www.cnblogs.com/wonz/p/12442704.html

时间: 2024-11-06 03:48:00

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

[leetcode]236. Lowest Common Ancestor of a Binary Tree二叉树最近公共祖先

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q

LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树求两点LCA)

题意:二叉树求两点LCA. /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* lowestCommonAncestor(TreeNode

671. Second Minimum Node In a Binary Tree 二叉树中第二小节点

Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly twoor zero sub-node. If the node has two sub-nodes, then this node's value is the smaller value among its two sub-nodes. G

leetcode 刷题之路 64 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 给出二叉树的中序遍历和后序遍历结果,恢复出二叉树. 后序遍历序列的最后一个元素值是二叉树的根节点的值,查找该元素在中序遍历序列中的位置mid,根据中序遍历和后序遍历性质,有: 位置mid以前的序列部分为二叉树根节点左子树中

UVALive 6577 Binary Tree 二叉树的LRU串

今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一个向下无限延伸的二叉树.然后输入起点(通过只含LRU的字符串S,从根结点开始执行).LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的). 然后输入一个可选步骤串T.可以选择T中的子序,从起点开始走.然后问可以走到多少个不同的结点. 比赛的时候不会做啊╮(╯

[LeetCode] 687. Longest Univalue Path 最长唯一值路径

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex

[LeetCode][JavaScript]Longest Increasing Path in a Matrix

Longest Increasing Path in a Matrix Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside of the boundary

【Leetcode】Longest Increasing Path in a Matrix

题目链接:https://leetcode.com/problems/longest-increasing-path-in-a-matrix/ 题目: Given an integer matrix, find the length of the longest increasing path. From each cell, you can either move to four directions: left, right, up or down. You may NOT move dia

[LeetCode] 687. Longest Univalue Path

Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. Note: The length of path between two nodes is represented by the number of edges between them. Ex