题解 | #重建二叉树#
合并二叉树
http://www.nowcoder.com/practice/7298353c24cc42e3bd5f0e0bd3d1d759
/*
- function TreeNode(x) {
- this.val = x;
- this.left = null;
- this.right = null;
- } */
/** *
- @param t1 TreeNode类
- @param t2 TreeNode类
- @return TreeNode类 */ function mergeTrees( t1 , t2 ) { if(t1 && !t2) { return t1 } if(t2 && !t1) { return t2 } if(!t1 && !t2) { return null } if(t1 && t2) { let t1Root = t1.val let t2Root = t2.val let node = new TreeNode(t1Root+t2Root) node.left = mergeTrees(t1.left,t2.left) node.right = mergeTrees(t1.right, t2.right) return node }
} module.exports = { mergeTrees : mergeTrees };