* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func middleNode(_ head: ListNode?...* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func middleNode(_ head: ListNode?...* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val
. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #...None # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val...=0 def convertBST(self, root: TreeNode) -> TreeNode: if root == None: return self.val...if root == None: return root.right = dfs(root.right) self.val...+= root.val root.val = self.val root.left = dfs(root.left) return
* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func reverseList(_ head: ListNode...* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val
. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #...Python3 实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...Python3 实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...Python3 实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...Python3 实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val
接下来我们使用自定义类型: from collections import defaultdict class Cls: def __init__(self, val='hello'): self.val...= val def __str__(self): return self.val def fun(val=121): return val dic_1 = defaultdict...from collections import defaultdict class Cls: def __init__(self, val='hello'): self.val...= val def __str__(self): return self.val dic = defaultdict(Cls) print(dic['a']== dic
* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func swapPairs(_ head: ListNode?)
. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left =...TreeNode :rtype: int """ # 层次遍历,找最左边的值 # 中序遍历,找最深一层最左边的节点 self.val...= root.val self.depth = 1 self.helper(root, 1) return self.val def helper...return self.helper(node.left, depth+1) if depth > self.depth: self.val
Python实现 2.1 使用栈 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val...return result 2.2 使用递归 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val...list 2.3 从头到尾遍历后逆序 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val...list[::-1] 2.4 栈+递归 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val
. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next =...Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val...快慢指针 # Definition for a binary tree node # class TreeNode: # def __init__(self, x): # self.val...None # # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val
# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x #...另一种思路:使用栈 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val =...输入一个链表,反转链表后,输出新链表的表头 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val...# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x #...:我们约定空树不是任意一个树的子结构) # -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val
翻转一个链表 #1 第一种方法:迭代 class ListNode(object): def __init__(self, x): self.val = x self.next...pre = p return pre #2 第二种方法:递归 class ListNode(object): def __init__(self, x): self.val
* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func reverseList(_ head: ListNode?)
. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #...= None class Solution: def kthSmallest(self, root: TreeNode, k: int) -> int: self.cnt, self.val...= 0, 0 self.inOrder(root, k) return self.val def inOrder(self, root: TreeNode...return self.inOrder(root.left, k) self.cnt += 1 if self.cnt == k: self.val...平衡二叉树 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val
. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #...maximum-depth-of-binary-tree/ # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...leetcode-98: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...-112: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val
* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func rotateRight(_ head: ListNode
Python3 实现: """ # Definition for a Node. class Node: def __init__(self, val, children): self.val...Python3 实现: """ # Definition for a Node. class Node: def __init__(self, val, children): self.val...Python3 实现: """ # Definition for a Node. class Node: def __init__(self, val, children): self.val...Ptthon3 实现: """ # Definition for a Node. class Node: def __init__(self, val, children): self.val...Python3 实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val
. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None #...Python实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...python实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...Python实现: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val...# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val
* public init() { self.val = 0; self.next = nil; } * public init(_ val: Int) { self.val = val...{ self.val = val; self.next = next; } * } */ class Solution { func mergeTwoLists(_ list1: ListNode
PartialOrd + Clone> TreeNode { fn do_insert(&self, val: T) -> AvlTreeImpl { if val < self.val...None), self.right.clone(), ) } } else if val > self.val...right) } } 删除 同样,类似插入: fn do_delete(&self, val: T) -> AvlTreeImpl { if val < self.val...else { // not found val None } } else if val > self.val...} } 查找 正常的二叉树查找: pub fn get_as_ref(&self, val: T) -> Option { if val < self.val
链表中倒数第 K 个结点 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val...链表中环的入口结点 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val =...# -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val = x #...合并两个排序的链表 # -*- coding:utf-8 -*- # class ListNode: # def __init__(self, x): # self.val =...# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x #
领取专属 10元无门槛券
手把手带您无忧上云