Skip to content

Latest commit

 

History

History
185 lines (156 loc) · 4.51 KB

File metadata and controls

185 lines (156 loc) · 4.51 KB

中文文档

Description

Given the root of a binary tree, return the length of the longest consecutive sequence path.

The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path needs to be from parent to child (cannot be the reverse).

 

Example 1:

Input: root = [1,null,3,2,4,null,null,null,5]
Output: 3
Explanation: Longest consecutive sequence path is 3-4-5, so return 3.

Example 2:

Input: root = [2,null,3,2,null,1]
Output: 2
Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2.

 

Constraints:

  • The number of nodes in the tree is in the range [1, 3 * 104].
  • -3 * 104 <= Node.val <= 3 * 104

Solutions

DFS.

Python3

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def longestConsecutive(self, root: TreeNode) -> int:
        def dfs(root, p, t):
            nonlocal ans
            if root is None:
                return
            t = t + 1 if p is not None and p.val + 1 == root.val else 1
            ans = max(ans, t)
            dfs(root.left, root, t)
            dfs(root.right, root, t)

        ans = 1
        dfs(root, None, 1)
        return ans

Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private int ans;

    public int longestConsecutive(TreeNode root) {
        ans = 1;
        dfs(root, null, 1);
        return ans;
    }

    private void dfs(TreeNode root, TreeNode p, int t) {
        if (root == null) {
            return;
        }
        t = p != null && p.val + 1 == root.val ? t + 1 : 1;
        ans = Math.max(ans, t);
        dfs(root.left, root, t);
        dfs(root.right, root, t);
    }
}

C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans;

    int longestConsecutive(TreeNode* root) {
        ans = 1;
        dfs(root, nullptr, 1);
        return ans;
    }

    void dfs(TreeNode* root, TreeNode* p, int t) {
        if (!root) return;
        t = p != nullptr && p->val + 1 == root-> val ? t + 1 : 1;
        ans = max(ans, t);
        dfs(root->left, root, t);
        dfs(root->right, root, t);
    }
};

Go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func longestConsecutive(root *TreeNode) int {
	ans := 1
	var dfs func(root, p *TreeNode, t int)
	dfs = func(root, p *TreeNode, t int) {
		if root == nil {
			return
		}
		if p != nil && p.Val+1 == root.Val {
			t++
			ans = max(ans, t)
		} else {
			t = 1
		}
		dfs(root.Left, root, t)
		dfs(root.Right, root, t)
	}
	dfs(root, nil, 1)
	return ans
}

func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

...