赞
踩
题目链接
第一次提交成功的代码
class Solution { public TreeNode mergeTrees(TreeNode t1, TreeNode t2) { if(t1 == null && t2 == null) return null; TreeNode newTreeNode = new TreeNode(); newTreeNode.val = ((t1==null?0:t1.val) + (t2==null?0:t2.val)); if(t1 == null){ t1 = new TreeNode(); } if(t2 == null){ t2 = new TreeNode(); } if(t1.left != null && t2.left != null){ newTreeNode.left = mergeTrees(t1.left,t2.left); } if(t1.right != null && t2.right != null){ newTreeNode.right = mergeTrees(t1.right,t2.right); } if(t1.left == null){ if(t2.left == null){ newTreeNode.left = null; } else{ newTreeNode.left = t2.left; } }else{ if(t2.left == null){ newTreeNode.left = t1.left; } } if(t1.right == null){ if(t2.right == null){ newTreeNode.right = null; } else{ newTreeNode.right = t2.right; } }else{ if(t2.right == null){ newTreeNode.right = t1.right; } } return newTreeNode; } }
题解链接
反思:
1、提交有两次失败,一次为叶子节点置空以后,调用方法时空指针异常,第二次为没有考虑根节点都为空的情况,导致三元运算中将值设为了0;
2、在看过题解以后,发现自己的代码中对空节点处理的思路过于死板,多出了很多无用代码。
题目链接
第一次提交ac代码
class Solution {
public int[] findMode(TreeNode root) {
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。