当前位置: 面试刷题>> 二叉树的最长连续子序列Ⅰ (经典算法题500道)
### 题目描述补充
题目:**二叉树的最长连续子序列Ⅰ**
给定一棵二叉树,其中每个节点的值都是非负整数。我们需要找到这棵树中所有从根节点到叶子节点的路径中,路径上节点值形成的最长连续递增子序列的长度。请编写一个函数来找出这个最长连续递增子序列的长度。
**注意**:
- 路径定义为从树的根节点开始,到任意一个叶子节点结束的路径。
- 叶子节点是没有子节点的节点。
- 如果不存在这样的路径,返回0。
### 示例
考虑以下二叉树:
```
1
/ \
2 3
/ \ \
2 3 4
\
5
```
最长连续递增子序列是 `[1, 2, 3, 4]`,所以返回4。
### PHP 示例代码
```php
val = $val;
$this->left = $left;
$this->right = $right;
}
}
function findLongestContinuousIncreasingPath($root) {
if ($root === null) return 0;
return dfs($root, $root->val, 1);
}
function dfs($node, $prevVal, $currLength) {
if ($node === null) return 0;
$maxLength = $currLength;
if ($node->val > $prevVal) {
$maxLength = max($maxLength, dfs($node->left, $node->val, $currLength + 1));
$maxLength = max($maxLength, dfs($node->right, $node->val, $currLength + 1));
} else {
$maxLength = max($maxLength, dfs($node->left, $node->val, 1));
$maxLength = max($maxLength, dfs($node->right, $node->val, 1));
}
return $maxLength;
}
// 使用示例
$root = new TreeNode(1);
$root->left = new TreeNode(2);
$root->right = new TreeNode(3);
$root->left->left = new TreeNode(2);
$root->left->right = new TreeNode(3);
$root->right->right = new TreeNode(4);
$root->left->right->right = new TreeNode(5);
echo findLongestContinuousIncreasingPath($root); // 输出 4
?>
```
### Python 示例代码
```python
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def findLongestContinuousIncreasingPath(root):
if not root:
return 0
return dfs(root, root.val, 1)
def dfs(node, prev_val, curr_length):
if not node:
return 0
max_length = curr_length
if node.val > prev_val:
max_length = max(max_length, dfs(node.left, node.val, curr_length + 1))
max_length = max(max_length, dfs(node.right, node.val, curr_length + 1))
else:
max_length = max(max_length, dfs(node.left, node.val, 1))
max_length = max(max_length, dfs(node.right, node.val, 1))
return max_length
# 使用示例
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(2)
root.left.right = TreeNode(3)
root.right.right = TreeNode(4)
root.left.right.right = TreeNode(5)
print(findLongestContinuousIncreasingPath(root)) # 输出 4
```
### JavaScript 示例代码
```javascript
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
function findLongestContinuousIncreasingPath(root) {
if (!root) return 0;
return dfs(root, root.val, 1);
}
function dfs(node, prevVal, currLength) {
if (!node) return 0;
let maxLength = currLength;
if (node.val > prevVal) {
maxLength = Math.max(maxLength, dfs(node.left, node.val, currLength + 1));
maxLength = Math.max(maxLength, dfs(node.right, node.val, currLength + 1));
} else {
maxLength = Math.max(maxLength, dfs(node.left, node.val, 1));
maxLength = Math.max(maxLength, dfs(node.right, node.val, 1));
}
return maxLength;
}
// 使用示例
let root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(2);
root.left.right = new TreeNode(3);
root.right.right = new TreeNode(4);
root.left.right.right = new TreeNode(5);
console.log(findLongestContinuousIncreasingPath(root)); // 输出 4
```
码小课网站中有更多关于算法和数据结构的内容分享给大家学习,希望对你有所帮助!