package
0.0.0-20241220224003-b7cf03a90b2b
Repository: https://github.com/szhou12/leetcode-go.git
Documentation: pkg.go.dev

# README

617. Merge Two Binary Trees

Solution idea

Post-order Traversal

  • Base Cases: (和LCA类型题有点像)

    1. 两棵树的当前node都为空
    2. tree1的当前node 不为空,tree2的当前node 为空: return tree1的当前node
    3. tree1的当前node 为空,tree2的当前node 不为空: return tree2的当前node
  • 后序遍历

    • 同时递归两棵树左孩子
    • 同时递归两棵树右孩子
    • 当前层:
      • 两棵树node的值求和
      • 重新链接返回的左、右孩子

Time complexity = $O(n)$

Resource

代码随想录-617.合并二叉树