var preorderTraversal = function(root) {let result=[]const pre=(node)=>{ if(!node) return [] result.push(node.val) pre(node.left) pre(node.right) } pre(root) return result};
var postorderTraversal = function(root) {
let result=[]
const after=(node)=>{
if(!node)
return []
after(node.left)
after(node.right)
result.push(node.val)
}
after(root)
return result
};
var inorderTraversal = function(root) {
let result=new Array()
const middleNode=(root)=>{
if(!root){
return []
}
middleNode(root.left)
result.push(root.val)
middleNode(root.right)
}
middleNode(root)
return result
};
/**
* @param {number} n
* @return {number}
*/
var numTrees = function(n) {
let G=new Array(n+1).fill(0)
G[0]=1
G[1]=1
for (let i = 2; i <= n; ++i) {
for (let j = 1; j <= i; ++j) {
G[i] += G[j - 1] * G[i - j];
}
}
return G[n]
};
var isBalanced = function(root) {
if(root===null)return true
else{
return Math.abs(height(root.left)-height(root.right))<=1&&isBalanced(root.left)&&isBalanced(root.right)
}
};
const height=(root)=>{
if(root===null) return 0
else return Math.max(height(root.left),height(root.right))+1
}
var kthLargest = function(root, k) {
let res=null
const remiddle=(root)=>{
if(!root){
return null
}
remiddle(root.right)
k--
if(!k) res=root.val
remiddle(root.left)
}
remiddle(root)
return res
};
var levelOrder = function(root) {
let result=[]
let queue=[]
if(!root) return result
queue.push(root)
while(queue.length){
let length=queue.length
let subres=[]
for(let i=0;i<length;i++){
let node=queue.shift()
subres.push(node.val)
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
}
result.push(subres)
}
return result
};
const height=(root)=>{
if(root===null) return 0
else return Math.max(height(root.left),height(root.right))+1
}
function hasPathSum( root , sum ) {
// write code here
if(!root) return false
if(!root.left&&!root.right&&sum-root.val===0)
return true
return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val)
}
module.exports = {
hasPathSum : hasPathSum
};
var isValidBST = function(root) {
let key=-Infinity
const middleNode=(root)=>{
if(!root){
return true
}
let l=middleNode(root.left)
if(root.val<=key) return false
key=root.val
let r=middleNode(root.right)
return l && r
}
return middleNode(root)
};
function isCompleteTree( root ) {
let queue=[]
let stop=false
queue.push(root)
while(queue.length){
let node=queue.shift()
if(!node){
stop=true
}else{
if(queue.length&&stop) return false
queue.push(node.left)
queue.push(node.right)
}
}
return true
}
function lowestCommonAncestor(root,p,q) {
if(root==null) return -1
if((p>=root.val&&q<=root.val)||(p<=root.val&&q>=root.val))
return root.val
else if(p<=root.val&&q<=root.val)
return lowestCommonAncestor(root.left,p,q)
else
return lowestCommonAncestor(root.right,p,q)
}
module.exports = {
lowestCommonAncestor : lowestCommonAncestor
};
function lowestCommonAncestor( root , o1 , o2 ) {
// write code here
const helper= (root, p, q) => {
if (root == null || root.val == o1 || root.val == o2)
return root;
const left = helper(root.left, o1, o2);
const right = helper(root.right, o1, o2);
//如果left为空,说明这两个节点在root结点的右子树上,我们只需要返回右子树查找的结果即可
if (left == null)
return right;
//同上
if (right == null)
return left;
//如果left和right都不为空,说明这两个节点一个在root的左子树上一个在root的右子树上,
//我们只需要返回cur结点即可。
return root;
}
return helper(root, o1, o2).val;
}
function reConstructBinaryTree(pre, vin)
{
// write code here
if(!pre.length||!vin.length) return null
let root=pre[0]
let i=vin.indexOf(root)
const node={
val:root,
left:reConstructBinaryTree(pre.slice(1,i+1), vin.slice(0,i)
),
right:reConstructBinaryTree(pre.slice(i+1),vin.slice(i+1))
}
return node
}
module.exports = {
reConstructBinaryTree : reConstructBinaryTree
};
function solve( xianxu , zhongxu ) {
// write code here
const constructor=(xianxu , zhongxu)=>{
if(!xianxu.length||!zhongxu.length){
return null
}
let root=xianxu[0]
let i=zhongxu.indexOf(root)
const node={
val:root,
left:constructor(xianxu.slice(1,i+1),zhongxu.slice(0,i)),
right:constructor(xianxu.slice(i+1),zhongxu.slice(i+1)),
}
return node
}
const root=constructor(xianxu , zhongxu)
if(!root) return[]
let queue=[]
let res=[]
queue.push(root)
while(queue.length){
let len=queue.length
let subres=[]
for(let i=0;i<len;i++){
let node=queue.shift()
subres.unshift(node.val)
if(node.left) queue.push(node.left)
if(node.right) queue.push(node.right)
}
res.push(subres[0])
}
return res
}
module.exports = {
solve : solve
};
function Convert(pRootOfTree)
{
let head=null
let pre=null
function dfs(cur){
if(cur===null) return
dfs(cur.left)
if(pre===null){
head=cur
}else{
pre.right=cur
}
cur.left=pre
pre=cur
dfs(cur.right)
}
dfs(pRootOfTree)
return head
}
function isSymmetrical(pRoot)
{
const isSame=(left,right)=>{
if(!left&&!right) return true
if(left==null||right==null||left.val!==right.val) return false
return isSame(left.left,right.right)&&isSame(left.right,right.left)
}
// write code here
return isSame(pRoot,pRoot)
}
function mergeTrees( t1 , t2 ) {
// write code here
if(!t1) return t2
if(!t2) return t1
t1.val+=t2.val
t1.left=mergeTrees( t1.left , t2.left )
t1.right=mergeTrees( t1.right , t2.right)
return t1
}
function Mirror( pRoot ) {
// write code here
if(!pRoot) return null
let temp= pRoot.left
let left=Mirror( pRoot.left )
let right=Mirror( pRoot.right )
pRoot.left=right
pRoot.right=left
return pRoot
}

var pathSum = function(root, target) {
let res=[]
let path=[]
const recur=(node,tar)=>{
if(!node) return
path.push(node.val)
tar=tar-node.val
if(!node.left&&!node.right&&tar===0){
res.push(path.slice(0))
}
recur(node.left,tar)
recur(node.right,tar)
path.pop()
}
recur(root,target)
return res
};