当前位置:网站首页>Merge binary tree
Merge binary tree
2022-07-22 04:08:00 【Gulu Gulu day】
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly
#
#
# @param t1 TreeNode class
# @param t2 TreeNode class
# @return TreeNode class
#
class Solution:
def mergeTrees(self , t1: TreeNode, t2: TreeNode) -> TreeNode:
# write code here
# Recursive base item : For the current two corresponding nodes :
# 1. If there is only one, return the other
# 2. If both exist, then sum
# 3. Then the left subtree and the right subtree of the node are recursive
if not t1:
return t2
if not t2:
return t1
t = TreeNode(t1.val + t2.val)
t.left = self.mergeTrees(t1.left, t2.left)
t.right = self.mergeTrees(t1.right, t2.right)
return t
边栏推荐
猜你喜欢
Thesis reading: dialogpt
2.2 sequential representation of linear table
论文阅读:DialoGPT
Relative entropy, information entropy and cross entropy
Shell programming - functions
Judge whether the binary tree is symmetric
What does the residual network solve and why is it effective abstract
Shell operators - mathematical operations, relational operations, string operations, file detection operations
薅羊毛的最高境界,解放你的双手
手握趣店100亿现金,罗敏决定再赌一把
随机推荐
5.2 access control
What is knowledge distillation? An introductory essay__ abstract
MySQL 优化系列(2)-- InnoDB重要参数优化
CountDownLatch与CyclicBarrier基本原理及区别
gadget之udc
libpng error: iTXt: chunk data is too large error: PNG unsigned integer out of range
Some skills about open, read, write, close in hook unistd
Judge whether the binary tree is symmetric
Shell programming - functions
xxl-job(2.3.0)分布式任务bean模式,GLUE shell调度实践,源码debug
为JTable添加按列选择功能
PWN的学习
团队协作api管理工具除了swagger,还有yapi
Word2vec (II)_ Implementation based on negative sampling method
EOS帐号体系
判断是否二叉搜索树
[CCF CSP] 201403-1 opposite number
3 pairs of easy to mix APIs for linkedblockingqueue
Chapter III after class exercises 15-23
Shell编程基础及变量