Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 18
Explanation: The red nodes are the nodes with even-value grandparent while the blue nodes are the even-value grandparents.
Constraints:
-
The number of nodes in the tree is between
1
and 1 0 4 10^4 104. -
The value of nodes is between
1
and100
.
方法一:自己写的,DFS。
方法二:别人写的,DFS,更精简。
import com.lun.util.BinaryTree.TreeNode;
public class SumOfNodesWithEvenValuedGrandparent {
//方法一:自己写的,DFS
public int sumEvenGrandparent(TreeNode root) {
if(root == null) return 0;
int result = 0;
if((root.val & 1) == 0) {
TreeNode left = root.left;
if(left != null) {
if(left.left != null)
result += left.left.val;
if(left.right != null)
result += left.right.val;
}
TreeNode right = root.right;
if(right != null) {
if(right.left != null)
result += right.left.val;
if(right.right != null)
result += right.right.val;
}
}
return result + sumEvenGrandparent(root.left) + sumEvenGrandparent(root.right);
}
//方法二:别人写的,DFS
public int sumEvenGrandparent2(TreeNode root) {
return helper(root, 1, 1);
}
public int helper(TreeNode node, int p, int gp) {
if (node == null) return 0;
return helper(node.left, node.val, p) + helper(node.right, node.val, p) + (gp % 2 == 0 ? node.val : 0);
}
总结一下
面试前要精心做好准备,简历上写的知识点和原理都需要准备好,项目上多想想难点和亮点,这是面试时能和别人不一样的地方。
还有就是表现出自己的谦虚好学,以及对于未来持续进阶的规划,企业招人
真题解析、进阶学习笔记、最新讲解视频、实战项目源码、学习路线大纲
详情关注公中号【编程进阶路】
更偏爱稳定的人。
万事开头难,但是程序员这一条路坚持几年后发展空间还是非常大的,一切重在坚持。
为了帮助大家更好更高效的准备面试,特别整理了《前端工程师面试手册》电子稿文件。
前端面试题汇总
JavaScript
性能
linux
前端资料汇总
前端工程师岗位缺口一直很大,符合岗位要求的人越来越少,所以学习前端的小伙伴要注意了,一定要把技能学到扎实,做有含金量的项目,这样在找工作的时候无论遇到什么情况,问题都不会大。