设计网站怎么做,怎么防止网站被注册机,wordpress 二级菜单样式,做网站展示软件提示#xff1a;文章写完后#xff0c;目录可以自动生成#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、力扣549. 二叉树中最长的连续序列二、力扣1325. 删除给定值的叶子节点 前言 像求和、求高度这种基本的二叉树函数很容易写#xff0c;有时候只要在它们的后… 提示文章写完后目录可以自动生成如何生成可参考右边的帮助文档 文章目录 前言一、力扣549. 二叉树中最长的连续序列二、力扣1325. 删除给定值的叶子节点 前言 像求和、求高度这种基本的二叉树函数很容易写有时候只要在它们的后序位置添加一点代码就能得到我们想要的答案。
一、力扣549. 二叉树中最长的连续序列
/*** 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 {int res 0;public int longestConsecutive(TreeNode root) {fun(root);return res;}public int[] fun(TreeNode root){if(root null){return new int[]{0,0};}int[] left fun(root.left);int[] right fun(root.right);int leftInc left[0], leftDes left[1];int rightInc right[0], rightDes right[1];int curInc 1, curDes 1;if(root.left ! null){if(root.left.val -1 root.val){curInc leftInc;}if(root.left.val 1 root.val){curDes leftDes;}}if(root.right ! null){if(root.right.val - 1 root.val){curInc Math.max(curInc,rightInc1);}if(root.right.val 1 root.val){curDes Math.max(curDes, rightDes1);}}res Math.max(res, curInc curDes -1);return new int[]{curInc,curDes};}
}二、力扣1325. 删除给定值的叶子节点
/*** 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 {public TreeNode removeLeafNodes(TreeNode root, int target) {if(root null){return null;}if(root.left null root.right null){if(root.val target){return null;}}TreeNode left removeLeafNodes(root.left,target);TreeNode right removeLeafNodes(root.right,target);if(left null right null){if(root.val target){return null;}}root.left left;root.right right;return root;}
}