[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历。

 

此处说明Divide and Conquer 的做法,其实跟recursive的做法很像,但是将结果存进array并且输出,最后conquer (这一步worst T:O(n)) 起来,所以时间复杂度可以从遍历O(n) -> O(n^2).

实际上代码是一样, 就是把[root.val] 放在先, 中, 后就是pre, in, post order了.

1) Preorder traversal

class Solution:
    def preOrder(self, root):
        if not root: return []
        left = self.preOrder(root.left)
        right = self.preOrder(root.right)
        return [root.val] + left + right  # worst T: O(n)

 

2) 

Inorder traversal

class Solution:
    def inOrder(self, root):
        if not root: return []
        left = self.preOrder(root.left)
        right = self.preOrder(root.right)
        return  left + [root.val] + right  # worst T: O(n)

 

3) Postorder traversal

class Solution:
    def postOrder(self, root):
        if not root: return []
        left = self.preOrder(root.left)
        right = self.preOrder(root.right)
        return  left + right + [root.val] # worst T: O(n)

 Maximum Depth of Binary Tree

Balanced Binary Tree

[LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

BInary Tree Maximum Path sum from root to leaf

[LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer

Binary Tree Level Order Traversal

Binary Tree Level Order Traversal II

Binary Tree Zigzag Level Order . Traversal

 

Binary Search Tree

 

Validate Binary Search Tree

Search Range in a Binary Search Tree

Inorder Successor in BST

Implement iterator of BInary Search Tree

Insert a Node in Binary Search Tree

Remove Node in Binary Search Tree

Steps:
1. Find the node

2. Find the maximum node in the left subtree

3. Replace the node with the maximum node in the left subtree.

Special Cases:
1. The node doest have a left child.

2. The maximum node in the left subtree has a left child.

3. The node is the root of the tree.

 

转载于:https://www.cnblogs.com/Johnsonxiong/p/10727545.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值